使用NUnit测试项目列表

问题描述:

如果我的概念错误,告诉我。我有2班; CountryState。一个州将有一个CountryId属性。使用NUnit测试项目列表

我有一个服务和库如下:

Service.cs

public LazyList<State> GetStatesInCountry(int countryId) 
    { 
     return new LazyList<State>(geographicsRepository.GetStates().Where(s => s.CountryId == countryId)); 
    } 

IRepository.cs

public interface IGeographicRepository 
{ 
    IQueryable<Country> GetCountries(); 

    Country SaveCountry(Country country); 

    IQueryable<State> 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(); 
    } 

我需要验证信息返回的州的重刑。我是否需要编写一个循环并在其中放置断言?

我不知道是否有东西在NUnit的这一点,但你可以使用LINQ做到这一点:后快速谷歌搜索,似乎你可以做到这一点

Assert.That(states.Select(c => c.CountryId), Is.All.EqualTo(1)); 

states.All(c => Assert.AreEqual(1, c.CountryId)) 

编辑

+0

它无效的LINQ。它告诉我Assert应该返回一个布尔值。 – 2010-12-19 19:13:35

+0

对不起,你应该像疯了一样指出 – 2010-12-19 19:19:21

Assert.IsTrue(states.All(x => 1 == x.CountryId));