Here is my integration test code:
[Fact(DisplayName = "Should only add AssetType when Name Provided")]
public async Task Test4()
{
using (var context = GetContext()) {
var listAssetTypes = await context.AssetType.ToListAsync();
Assert.Equal(0, listAssetTypes.Count);
var goodAssetType = new AssetType { Name = "1st Item" };
context.Add(goodAssetType);
await context.SaveChangesAsync();
listAssetTypes = await context.AssetType.ToListAsync();
Assert.Equal(1, listAssetTypes.Count);
var badAssetType = new AssetType {};
context.Add(badAssetType);
await context.SaveChangesAsync();
listAssetTypes = await context.AssetType.ToListAsync();
Assert.Equal(1, listAssetTypes.Count);
}
}
The first two assert's pass. The 3rd fails, Actual is 2, and if I debug I can see a new Id has been assigned and the name is set to null.
Here is my context method:
private WorldContext GetContext()
{
var builder = new ConfigurationBuilder();
var config = builder.Build();
var options = new DbContextOptionsBuilder<WorldContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
var context = new WorldContext(config, options);
return context;
}
And my model:
public class AssetType
{
[DatabaseGenerated(databaseGeneratedOption: DatabaseGeneratedOption.Identity)]
[Key]
public int AssetTypeId { get; set; }
[Required]
public string Name { get; set; }
}