如何使用 moq 和 mspec(BDD 风格)比较两个对象列表

发布于 2024-08-18 22:11:20 字数 1231 浏览 5 评论 0原文

我刚刚开始为我当前的项目探索 BDD。我使用 moq 和 mspec 来获得良好的测试输出。 但是,我找不到足够的例子来说明我正在尝试做的事情,也许我使用了错误的方法,或者可能只是不太理解语法,请指教。

问题是我正在尝试验证是否已在类中设置属性。该属性是一个对象列表,我想验证每个对象是否具有与预期对象相同的值。

因此,鉴于最初的前提,

public class Data
{
    int a;      
}

public class DataViewer : IDataViewer
{
    public List<Data> dataList {get;set;}
}

public interface IDataViewer
{
    public List<Data> dataList {get;set;}
}

我使用 mspec 和 moq 来执行以下操作:

class when_refreshing_data_list : Context
{
    Because .... = () =>
    {
        ....  // process the datalist
    }

    ThenIt should_set_the_data_list = () =>
    {
        List<DataList> expectedDataList = new List<DataList>();
        expectedDataList.add() // add some expected values to the datalist
        ...

        // problem is here in comparing two List<DataList>
        _mockDataViewer.VerifySet(f => f.dataList = expectedDataList)
    }
}

public abstract class Context
{
    Establish context = () =>
    {
         _mockDataViewer = new Mock<IDataViewer>();
    }
}

我将如何执行此比较? 我可以使用运算符覆盖吗?如果是,怎么办? 我试图将表达式主体放在 lambda 的右侧,结果却被告知“带有语句主体的 lambda 表达式无法转换为表达式树”。

任何帮助表示赞赏:)

I've just started to explore BDD for my current project. I'm using moq with mspec to get nice test outputs.
However, i can't find enough examples of what i'm trying to do, maybe i'm using the wrong approach or perhaps just don't understand the syntax enough, please advise.

The problem is i'm trying to verify whether a property has been set in a class. This property is a list of objects and i want to verify that each object has the same values as the expected object.

So given the initial premises

public class Data
{
    int a;      
}

public class DataViewer : IDataViewer
{
    public List<Data> dataList {get;set;}
}

public interface IDataViewer
{
    public List<Data> dataList {get;set;}
}

I'm using mspec and moq to do the following

class when_refreshing_data_list : Context
{
    Because .... = () =>
    {
        ....  // process the datalist
    }

    ThenIt should_set_the_data_list = () =>
    {
        List<DataList> expectedDataList = new List<DataList>();
        expectedDataList.add() // add some expected values to the datalist
        ...

        // problem is here in comparing two List<DataList>
        _mockDataViewer.VerifySet(f => f.dataList = expectedDataList)
    }
}

public abstract class Context
{
    Establish context = () =>
    {
         _mockDataViewer = new Mock<IDataViewer>();
    }
}

How would I perform this comparison?
Can I use operator override? If yes, how?
I've tried to put an expression body on the right side of the lambda only to be told that "a lambda expression with a statement body cannot be converted to an expression tree".

Any help appreciated :)

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

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

发布评论

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

评论(1

流年里的时光 2024-08-25 22:11:20

您是否重载了 Data 对象上的 equals 方法?

public override bool Equals(object obj)
{
    if (((Data)obj).a.Equals(this.a))
        return true;

    return false;
}

当您将 dataList 与其预期的 DataList 进行比较时,应该会自动调用此函数。

Have you overloaded the equals method on the Data object?

public override bool Equals(object obj)
{
    if (((Data)obj).a.Equals(this.a))
        return true;

    return false;
}

This should get called automatically when you're comparing the dataList to its expectedDataList.

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