如何设置测试方法以模拟接受对象的存储库?
目前为止,这是我所拥有的:
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);
返回一个整数。
我需要做两件事:
- 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");
框架:
- NUnit。
- Moq。
- ASP.NET MVC - 存储库模式。
AddCountry("Jamaica")
两次并设置仓库来监视同一个字符串是否被传递了两次? - Shawn Mclean