如何使用 moq 和 mspec(BDD 风格)比较两个对象列表
我刚刚开始为我当前的项目探索 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否重载了 Data 对象上的 equals 方法?
当您将 dataList 与其预期的 DataList 进行比较时,应该会自动调用此函数。
Have you overloaded the equals method on the Data object?
This should get called automatically when you're comparing the dataList to its expectedDataList.