我仍然会模拟查找表,即使对于“快乐”的测试场景,您只需返回从数据库返回的真实数据。
但是您可以灵活地测试边缘情况,例如从数据库中删除数据的情况,或者是否没有返回数据。
例如
// Happy tests:
Mock.Setup for GetLookupData() => return FetchRealDataHere();
Assert.AreEqual(3, Mock.Object.CountOfSizes()); // Ensure that Small, Medium and Large
... Do Good scenario unit Tests here
// Test Edge cases / destructive tests
Mock.Setup for GetLookupData() => return new Collection() [{ Small, 1}] [{ Medium 2}] - // But omit large
... Exception case Unit tests here, e.g. Try and Insert a Large product here.
// Test empty collection
Mock.Setup for GetLookupData() => return new Collection() [Empty]
// Assert that "NoItemsFoundException" was thrown by your logic here
// Handle empty collection
Mock.Setup for GetLookupData() => return new Collection() [Empty]
编辑我已经为下面的更新评论更新了伪模拟设置/单元测试代码。