最小起订量无效方法
我正在尝试弄清楚这个 Mock(使用 Moq 框架)是如何工作的,但我对返回 void 的方法有点困惑。
原始对象具有以下方法/属性:
void Add(Person entity);
void Save();
IQueryable<Person> Persons;
Add 方法调用 InsertOnSubmit
(它是 Linq to sql),Save 方法调用 Context.SubmitChanges()
。 Person 的属性返回 Table
对象。
然而,我不确定如何模拟这些方法,因为我显然不能使用 Returns() 。
或者我的设计意味着我实际上无法正确模拟对象?
I'm trying to work out how this Mock (using the Moq framework) stuff all works, but I've become a bit confused with methods that return void.
The original object has the following methods/properties:
void Add(Person entity);
void Save();
IQueryable<Person> Persons;
The Add method calls InsertOnSubmit
(it's Linq to sql), and the Save method calls Context.SubmitChanges()
. The Person's property returns the Table<News>
object.
I'm not sure how I go about mocking these methods however, as I obviously can't use Returns()
.
Or does my design mean that I can't actually Mock the objects properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您打算测试什么。
当您想要测试正在测试的类与其他类之间的交互时,起订量可以为您提供很大帮助。
在您描述的情况下,您可能正在测试一个使用具有上述方法和属性的类的类。要验证交互,您需要检查是否使用预期参数调用了
Add
方法、是否调用了Save
方法以及是否调用了Persons
属性。参考以读取结果。这可以通过以下方式实现:如果您想测试其他内容并提供这些方法的存根实现,那么您可能只想创建一个测试实现或考虑使用 Moq 设置,其函数表示为lambdas 而不是简单的设置。
It depends on what you intend to test.
Moq can help you significantly when you want to test interactions between the class being tested and other classes.
In the case you are describing you are presumably testing a class which is using class with methods and properties mentioned above. To verify interactions you want to check if the
Add
method is called with expected parameters, that theSave
method is called and that thePersons
property is refered to in order to read results. This can be achieved in the following way:If you want to test something else and provide a stub implementation of these methods then you may want to just create a test implementation or consider using Moq setups with functions expressed as lambdas rather than simple setups.