我有一项在DB中添加城市的服务。它运作良好,现在我需要测试。我以前从未进行过测试。感谢您的建议。我会测试用例-如果它不为null-测试通过(我的意思是如果用户插入任何内容-测试通过,如果他不插入数据-测试失败)。我只需要检查townName为null或no。这是我的TestClass
[TestClass]
public partial class TownRepositoryTests
{
[TestMethod]
public async Task InsertTownName_ReturnTownName()
{
var builder = new RepositoryBuilder<TownRepository>().SetupManager();
using (builder.CreateIsolationScope())
{
var expectedTown = await TestBuilder.SetupTownNameAsync(builder);
var repository = builder.Build();
var townName = expectedTown.Name;
var result = await repository.InsertTown(townName);
Assert.IsNotNull(result);
}
}
}
SetupTownNameAsync方法(在这里我添加数据)。我认为它很好用,因为当我使用调试测试时,我检查了此方法是否返回(id = 1,name =“ TestTown”)
public static class TestBuilder
{
public static async Task<Town> SetupTownNameAsync(RepositoryBuilder<TownRepository> builder)
{
var town = await builder.CreateTownAsync(name: "TestTown");
var setupResult = new Town
{
Id = town.Id,
Name = town.Name
};
return setupResult;
}
}
这是我的InsertTown方法
public async Task<int> InsertTown(string townName)
{
var parameters = new { townName };
return await Connection.QueryFirstOrDefaultAsync<int>(Adhoc["AddTown"], parameters, Transaction);
}
当我检查调试测试问题时-此行返回“ TestTown”,但是当我在断言中检查时-我的测试失败
var result = await repository.InsertTown(townName);
Assert.IsNotNull(result);