使用 NUnit 测试项目列表

发布于 2024-10-08 08:14:33 字数 1578 浏览 2 评论 0原文

告诉我我的概念是否错误。我有2节课; 国家/地区。一个州将有一个 CountryId 属性。

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

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();
    }

我需要验证返回的状态信息。我需要编写一个循环并将断言放入其中吗?

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]
    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();
    }

I need to verify the information of the states returned. Do I need to write a loop and put the asserts in it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉 2024-10-15 08:14:33

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

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

懷念過去 2024-10-15 08:14:33

我不知道 nunit 中是否有这样的东西,但你可以使用 linq 来做到这一点:

    states.All(c => Assert.AreEqual(1, c.CountryId))

编辑
快速谷歌搜索后似乎你可以做到这一点

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文