2010-12-19 116 views
2

如果我的概念錯誤,告訴我。我有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(); 
    } 

我需要驗證信息返回的州的重刑。我是否需要編寫一個循環並在其中放置斷言?

回答

1

我不知道是否有東西在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

3

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