public class ProductServiceTests
{
[Fact]
public void GetProductById_ReturnsProduct_WhenProductExists()
{
// Arrange
var productId = 1;
var expectedProduct = new Product { Id = 1, Name = "Test" };
var mockRepo = new Mock<IProductRepository>();
mockRepo.Setup(r => r.GetByIdAsync(productId))
.ReturnsAsync(expectedProduct);
var service = new ProductService(mockRepo.Object);
// Act
var result = service.GetProductByIdAsync(productId).Result;
// Assert
Assert.NotNull(result);
Assert.Equal(expectedProduct.Name, result.Name);
}
[Theory]
[InlineData(0, false)]
[InlineData(1, true)]
[InlineData(100, true)]
public void IsValidPrice_ReturnsExpected(int price, bool expected)
{
var service = new ProductService(null);
var result = service.IsValidPrice(price);
Assert.Equal(expected, result);
}
}
public class TestDatabaseFixture : IDisposable
{
private readonly SqliteConnection _connection;
public AppDbContext CreateContext()
=> new(new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(_connection).Options);
public TestDatabaseFixture()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
using var context = CreateContext();
context.Database.EnsureCreated();
}
public void Dispose() => _connection.Dispose();
}