2014-09-11 57 views
1

注意>国家/地区表中的记录数:36条记录。存储库测试不会返回预计的实体数量

我的代码:

[TestFixture] 
    public class CountriesControllerTest 
    { 
     Mock<IJN_CountryRepository> countryRepository; 
     Mock<IUnitOfWork> unitOfWork; 

     IJN_CountryService countryService; 

     [SetUp] 
     public void SetUp() 
     { 
      countryRepository = new Mock<IJN_CountryRepository>(); 
      unitOfWork = new Mock<IUnitOfWork>(); 
      countryService = new JN_CountryService(countryRepository.Object, unitOfWork.Object); 
     } 
     [Test] 
     public void ManyDelete() 
     { 
      var count = countryService.GetCount(); 
      // Assert 
      Assert.AreEqual(36, count); 
     } 
    } 

NUnit测试消息:

enter image description here

为什么?为什么不读取记录的数量?

回答

0

有了这两条线

countryRepository = new Mock<IJN_CountryRepository>(); 
unitOfWork = new Mock<IUnitOfWork>(); 

您创建了一个对象,对象有没有逻辑,也不对任何数据库的任何知识。这些是mocks。你需要指导他们做什么,以使他们的工作,如:

var sampleCountries = Create36SampleCountries(); 
countryRepository = new Mock<IJN_CountryRepository>(); 
countryRepository.Setup(m => m.Countries).Returns(sampleCountries); 

为了测试你不应该使用嘲笑,但实际仓库被真正的数据库工作(注意,这然后将是一个集成测试)。

相关问题