Moq.Setup 未返回预期集合?
我有以下代码:
var service = new Mock<INavigationService>();
service.Setup(x => x.GetSchemes(new SchemeFilterEntity())).Returns(new List<SchemeEntity>
{
new SchemeEntity
{
Id = 1,
Name = "Test"
},
new SchemeEntity
{
Id = 2,
Name = "Test 2"
}
});
var sut = service.Object;
var sut = service.GetSchemes(new SchemeFilterEntity());
但是,当调用 GetSchemes 方法时,它返回 null?
有什么想法吗?
I have the following code:
var service = new Mock<INavigationService>();
service.Setup(x => x.GetSchemes(new SchemeFilterEntity())).Returns(new List<SchemeEntity>
{
new SchemeEntity
{
Id = 1,
Name = "Test"
},
new SchemeEntity
{
Id = 2,
Name = "Test 2"
}
});
var sut = service.Object;
var sut = service.GetSchemes(new SchemeFilterEntity());
However when the GetSchemes method is called it returns null?
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信应该是
因为否则,起订量将寻找“newSchemeFilterEntity()”的确切实例你传入了 setup 方法,它永远不会匹配其他任何东西。
编辑:也就是说,你的 sut 不应该是你正在嘲笑的东西,它应该是使用你的模拟对象的东西。
I believe that should be
because otherwise, moq will be looking for that exact instance of the 'new SchemeFilterEntity()' that you passed in in the setup method, which will never match anything else.
Edit: That said, your sut should not be the thing you are mocking, it should be the thing that's using your mocked object.
我不知道您要测试什么,但如果您想“覆盖”
GetSchema
的行为使用模拟对象,该方法必须是类上的虚拟方法
如果您想使用模拟对象来存根 INavigationService,您必须
在设置中执行以下操作,您还应该使用它。是任意< SchemeFilterEntity >() 而不是创建一个具体的对象
I don't know what you are tring to test but if you want "Override" the behavior of
GetSchema
using a mocked object that method must be
virtual
on the classIf you want to use the mocked object to stub out the INavigationService you have to do the below
in your setup you should also use
It.IsAny< SchemeFilterEntity >()
instead of create a concrete object