AutoMocker 问题

发布于 2024-10-08 05:20:36 字数 337 浏览 0 评论 0原文

我正在尝试测试我的控制器,但每个控制器都依赖于一个服务,而该服务又依赖于存储库。我有以下代码...

Mock.Get(controller.Get<ServiceInterface>())
            .Setup(s => s.GetData())
            .Returns(FakeData.Create<Entity>(25));

我不断收到与 ServiceInterface 具体类相关的错误,该类没有其存储库的默认实例(已注入)。我试图避免创建 FakeService,但看起来这就是我必须要做的。有人可以提供一些建议吗?

I'm trying to test my controllers, but each of them have a dependency on a service which has a dependency on a repository. I have the following code...

Mock.Get(controller.Get<ServiceInterface>())
            .Setup(s => s.GetData())
            .Returns(FakeData.Create<Entity>(25));

I keep getting an error related to the ServiceInterface concrete class not having a default instance for its repository (injected). I'm trying to avoid creating a FakeService, but its looking like that's what I'm going to have to do. Can anyone offer some suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

回眸一遍 2024-10-15 05:20:36

我对此问题的回答展示了如何对服务进行编程要实现接口,请使用该接口作为依赖项,然后模拟该服务以帮助您进行单元测试。

编辑

由于没有任何使用 MoqAutoMocker 的经验,我的建议有些有限。但我会首先尝试直接使用 Moq,看看这是否会给您带来任何麻烦。

var serviceMock = new Mock<IService>();
serviceMock.Setup(r => r.GetData())
    .Returns(FakeData.Create<Entity>(25));
var controller = new MyController(serviceMock.Object);

一旦你确保这有效,你就可以引入它的自动模拟方面:

var autoMocker = new MoqAutoMocker<MyController>();
Mock.Get(autoMocker.Get<IService>()).Setup(r => r.GetData())
    .Returns(FakeData.Create<Entity>(25));
MyController controller = autoMocker.ClassUnderTest;

My response to this question shows how you can program your services to fulfill an interface, use that interface as a dependency, and then mock the service to help you in unit testing.

Edit

Not having had any experience with the MoqAutoMocker, my advice is somewhat limited. But I would begin by attempting to use Moq directly, and see if that's causing you any trouble.

var serviceMock = new Mock<IService>();
serviceMock.Setup(r => r.GetData())
    .Returns(FakeData.Create<Entity>(25));
var controller = new MyController(serviceMock.Object);

Once you've ensured that this works, you can introduce the auto-mocking aspect of it:

var autoMocker = new MoqAutoMocker<MyController>();
Mock.Get(autoMocker.Get<IService>()).Setup(r => r.GetData())
    .Returns(FakeData.Create<Entity>(25));
MyController controller = autoMocker.ClassUnderTest;
盗心人 2024-10-15 05:20:36

您想要测试哪些类以及您想要剔除哪些类?我猜您正在尝试测试您的控制器,并希望停止您的服务。如果是这种情况,那么服务的具体实现及其依赖项根本不应该参与您的测试,并且应该是无关的。您应该只需要存根测试控制器所需的服务接口的行为。

我只是猜测您的意图,所以如果我的答案没有意义,您可能需要发布更多代码来显示您要测试的内容。

What classes are you trying to test and what classes are you trying to stub out? I would guess that you are trying to test your controller, and want to stub out your service. If that is the case, the concrete implementation of your service, and therefore its dependencies, should not participate in your test at all and should be irrelevant. You should only need to stub out the behavior of the service interface that is needed to test your controller.

I'm only guessing at your intention, so if my answer doesn't make sense, you may need to post more code showing what you are trying to test.

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