【发布时间】:2011-05-27 17:49:49
【问题描述】:
如何在模拟接受对象的存储库上设置我的测试方法?
这是我目前所拥有的:
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); 返回一个整数。
我需要做两件事:
- 第一次测试,我需要告诉设置返回 1 的 int。
-
我需要创建第二个测试
Insert_Duplicate_Country_Throws_Exception()。在我的设置中,我如何告诉存储库在我这样做时抛出错误:int id = geoService.AddCountry("Jamaica"); int id = geoService.AddCountry("Jamaica");
框架:
- NUnit。
- 起订量。
- ASP.NET MVC - 存储库模式。
【问题讨论】:
标签: unit-testing nunit moq