Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 使用NUnit测试项目列表_Unit Testing_Nunit_Moq - Fatal编程技术网

Unit testing 使用NUnit测试项目列表

Unit testing 使用NUnit测试项目列表,unit-testing,nunit,moq,Unit Testing,Nunit,Moq,告诉我,如果我的想法是错误的。我有两个班国家和州。州将有一个CountryId属性 我有一个服务和一个存储库,如下所示: 服务中心 public LazyList<State> GetStatesInCountry(int countryId) { return new LazyList<State>(geographicsRepository.GetStates().Where(s => s.CountryId == country

告诉我,如果我的想法是错误的。我有两个班<代码>国家和
。州将有一个
CountryId
属性

我有一个服务和一个存储库,如下所示:

服务中心

    public LazyList<State> GetStatesInCountry(int countryId)
    {
        return new LazyList<State>(geographicsRepository.GetStates().Where(s => s.CountryId == countryId));
    }
public LazyList GetStatesInCountry(intcountryid)
{
返回新的LazyList(geographicsRepository.GetStates(),其中(s=>s.CountryId==CountryId));
}
IRepository.cs

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

    Country SaveCountry(Country country);

    IQueryable<State> GetStates();

    State SaveState(State state);
}
公共接口IGeographicRepository
{
IQueryable GetCountries();
国家储蓄国(Country-Country);
IQueryable 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();
    }
private IQueryable getStates()
{
列表状态=新列表();
states.Add(新的State(1,1,“Manchester”);//参数为:StateId、CountryId和StateName
添加(新州(2,1,“圣伊丽莎白”);
增加(新的州(2,2,“圣露西”);
返回状态。AsQueryable();
}
[测试]
public void可获取国家/地区的州/州列表()
{
const int countryId=1;
//设置
geographicsRepository.Setup(x=>x.GetStates()).Returns(GetStates());
//召唤
var states=geoService.GetStatesInCountry(countryId);
//断言
声明.声明(国家);
//我如何在这里写一个断言来检查返回的状态是否为CountryId=CountryId?
geographicsRepository.VerifyAll();
}

我需要核实返回的州的信息。我是否需要编写一个循环并将断言放入其中?

我不知道nunit中是否有用于此的内容,但您可以使用linq执行此操作:

    states.All(c => Assert.AreEqual(1, c.CountryId))
编辑 在快速谷歌搜索之后,你似乎可以做到这一点

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

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

它不是有效的LINQ。它告诉我断言应该返回布尔值。