开发者

How to compare two lists of objects using moq with mspec (BDD style)

开发者 https://www.devze.com 2022-12-18 05:29 出处:网络
I\'ve just started to explore BDD for my current project. I\'m using moq with mspec to get nice test outputs.

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 :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消