MOQ 可以模拟一个类吗
MOQ 一个不继承接口的类是否合法,如下所示:
var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
我将 IService 注入到 ValidAgeRule 中,这只是一个简单的类,具有一个名为“Excute”的方法。我的问题是如何验证是否已被调用。每当我尝试时:
mockValidAgeRule.Verify(x => x.Execute(app)); //Where App is a valid object
有人知道该怎么做吗?
is it legal to MOQ a class that does not inherit an interface like so:
var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
I inject the IService into ValidAgeRule which is just a simple class with one method called "Excute". My question is how to I verify that has been called. Whenever I try:
mockValidAgeRule.Verify(x => x.Execute(app)); //Where App is a valid object
does anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你需要使你的执行方法虚拟。
我的理解是,在这种情况下,Moq 创建您的类的子类,并且需要重写您的方法以跟踪它是否已被调用。
I believe you need to make your Execute method virtual.
My understanding is that Moq creates a subclass of your class in this case, and needs to override your method in order to keep track of whether it has been called.