2014-09-11 45 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); 

爲了測試你不應該使用嘲笑,但實際倉庫被真正的數據庫工作(注意,這然後將是一個集成測試)。

相關問題