2010-12-19 9 views
2

私の考えが間違っているかどうか教えてください。私には2つのクラスがあります。 CountryおよびState。状態には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)) 

EDIT

+0

無効なLINQです。 Assertはbooleanを返すべきだと私に伝えています。 –

+0

申し訳ありませんが、あなたは正しいです、それは狂ったように指摘されるべきです –

3

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

関連する問題