2010-12-19 3 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가 boolean 값을 리턴해야한다고 알려줍니다. –

+0

미안하지만 네가 맞다면 미친 것처럼 지적해야한다. –

3

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

관련 문제