Moq.Setup 未返回预期集合?

发布于 2024-12-26 12:30:19 字数 691 浏览 0 评论 0原文

我有以下代码:

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2025-01-02 12:30:19

我相信应该是

service.Setup(x => x.GetSchemes(It.IsAny())).Returns.....

因为否则,起订量将寻找“newSchemeFilterEntity()”的确切实例你传入了 setup 方法,它永远不会匹配其他任何东西。

编辑:也就是说,你的 sut 不应该是你正在嘲笑的东西,它应该是使用你的模拟对象的东西。

I believe that should be

service.Setup(x => x.GetSchemes(It.IsAny< SchemeFilterEntity >())).Returns.....

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.

空‖城人不在 2025-01-02 12:30:19

我不知道您要测试什么,但如果您想“覆盖”GetSchema 的行为
使用模拟对象,该方法必须是类上的虚拟方法

如果您想使用模拟对象来存根 INavigationService,您必须

   .........

    var sut = service.Object;

    SomeThing.UseNavigavtionService(sut); //this is supposed to be the class which you will test.Sut is a mocked 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 class

If you want to use the mocked object to stub out the INavigationService you have to do the below

   .........

    var sut = service.Object;

    SomeThing.UseNavigavtionService(sut); //this is supposed to be the class which you will test.Sut is a mocked INavigationService

in your setup you should also use It.IsAny< SchemeFilterEntity >() instead of create a concrete object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文