简体   繁体   中英

Repository test doesn't return expected number of entities

Note > The number of records in the Country Table: 36 records.

My code :

[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 Test Message :

在此处输入图片说明

Why? Why not read the number of records?

With these two lines

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

you created a fake objects, objects that have no logic nor any knowledge about any databases. These are mocks . You need to instruct them what to do in order to make them work, like:

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

In order for your test to work with real database you should not be using mocks but your actual repositories (note that this would then be an integration test).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM