Tell me if my concept is wrong. I have 2 classes; Country
and State
. A state will have a CountryId
property.
I have a service and a repository as follows:
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]
p开发者_JAVA技巧ublic 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();
}
I need to verify the information of the states returned. Do I need to write a loop and put the asserts in it?
Assert.IsTrue(states.All(x => 1 == x.CountryId));
I don't know if there is something in nunit for this, but you could do this with linq:
states.All(c => Assert.AreEqual(1, c.CountryId))
EDIT after quick googling it seems you can do this
Assert.That(states.Select(c => c.CountryId), Is.All.EqualTo(1));
精彩评论