Unit testing 使用NUnit测试项目列表
告诉我,如果我的想法是错误的。我有两个班<代码>国家和Unit testing 使用NUnit测试项目列表,unit-testing,nunit,moq,Unit Testing,Nunit,Moq,告诉我,如果我的想法是错误的。我有两个班国家和州。州将有一个CountryId属性 我有一个服务和一个存储库,如下所示: 服务中心 public LazyList<State> GetStatesInCountry(int countryId) { return new LazyList<State>(geographicsRepository.GetStates().Where(s => s.CountryId == country
州
。州将有一个CountryId
属性
我有一个服务和一个存储库,如下所示:
服务中心
public LazyList<State> GetStatesInCountry(int countryId)
{
return new LazyList<State>(geographicsRepository.GetStates().Where(s => s.CountryId == countryId));
}
public LazyList GetStatesInCountry(intcountryid)
{
返回新的LazyList(geographicsRepository.GetStates(),其中(s=>s.CountryId==CountryId));
}
IRepository.cs
public interface IGeographicRepository
{
IQueryable<Country> GetCountries();
Country SaveCountry(Country country);
IQueryable<State> GetStates();
State SaveState(State state);
}
公共接口IGeographicRepository
{
IQueryable GetCountries();
国家储蓄国(Country-Country);
IQueryable GetStates();
State SaveState(State State);
}
MyTest.cs
private IQueryable<State> getStates()
{
List<State> states = new List<State>();
states.Add(new State(1, 1, "Manchester"));//params are: StateId, CountryId and StateName
states.Add(new State(2, 1, "St. Elizabeth"));
states.Add(new State(2, 2, "St. Lucy"));
return states.AsQueryable();
}
[Test]
public void Can_Get_List_Of_States_In_Country()
{
const int countryId = 1;
//Setup
geographicsRepository.Setup(x => x.GetStates()).Returns(getStates());
//Call
var states = geoService.GetStatesInCountry(countryId);
//Assert
Assert.IsInstanceOf<LazyList<State>>(states);
//How do I write an Assert here to check that the states returned has CountryId = countryId?
geographicsRepository.VerifyAll();
}
private IQueryable getStates()
{
列表状态=新列表();
states.Add(新的State(1,1,“Manchester”);//参数为:StateId、CountryId和StateName
添加(新州(2,1,“圣伊丽莎白”);
增加(新的州(2,2,“圣露西”);
返回状态。AsQueryable();
}
[测试]
public void可获取国家/地区的州/州列表()
{
const int countryId=1;
//设置
geographicsRepository.Setup(x=>x.GetStates()).Returns(GetStates());
//召唤
var states=geoService.GetStatesInCountry(countryId);
//断言
声明.声明(国家);
//我如何在这里写一个断言来检查返回的状态是否为CountryId=CountryId?
geographicsRepository.VerifyAll();
}
我需要核实返回的州的信息。我是否需要编写一个循环并将断言放入其中?我不知道nunit中是否有用于此的内容,但您可以使用linq执行此操作:
states.All(c => Assert.AreEqual(1, c.CountryId))
编辑
在快速谷歌搜索之后,你似乎可以做到这一点
Assert.That(states.Select(c => c.CountryId), Is.All.EqualTo(1));
Assert.IsTrue(states.All(x=>1==x.CountryId)) 它不是有效的LINQ。它告诉我断言应该返回布尔值。