How do I set up my test method on that mocks a repository which accepts an object?
This is what I have so far:
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);
returns an int.
I need to do 2 things:
- First test, I need to tell the setup to return an int of 1.
I need to create a second test
Insert_Duplicate_Country_Throws_Exception()
. In my Setup, how do I tell the repository to throw an error when I do:int id = geoService.AddCountry("Jamaica"); int id = geoService.AddCountry("Jamaica");
Framework:
- NUnit.
- Moq.
- ASP.NET MVC - repository pattern.