Использование Moq для имитации репозитория, возвращающего значение

Как мне настроить свой тестовый метод на имитирующем репозиторий, принимающий объект?

Это что у меня есть:

Service.cs

    public int AddCountry(string countryName)
    {
        Country country = new Country();
        country.CountryName = countryName;
        return geographicsRepository.SaveCountry(country).CountryId;
    }

test.cs

    [Test]
    public void Insert_Country()
    {
        //Setup
        var geographicsRepository = new Mock<IGeographicRepository>();

        geographicsRepository.Setup(x => x.SaveCountry(It.Is<Country>(c => c.CountryName == "Jamaica"))); //How do I return a 1 here?

        GeographicService geoService = new GeographicService(geographicsRepository.Object);

        int id = geoService.AddCountry("Jamaica");

        Assert.AreEqual(1, id);
    }

SaveCountry (Country country); возвращает int.

Мне нужно сделать 2 вещи:

  1. Первый тест, Мне нужно указать установке, чтобы она возвращала int, равное 1.
  2. Мне нужно создать второй тест Insert_Duplicate_Country_Throws_Exception () . В моей настройке, как мне указать репозиторию выдавать ошибку, когда я это делаю:

     int id = geoService.AddCountry ("Jamaica");
    int id = geoService.AddCountry («Ямайка»);
    

Платформа:

  1. NUnit.
  2. Moq.
  3. ASP.NET MVC - шаблон репозитория.
8
задан Shawn Mclean 19 December 2010 в 03:02
поделиться