最小起订量 测试返回类型

发布于 2024-09-24 08:10:42 字数 464 浏览 3 评论 0原文

鉴于以下情况:

var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
mockValidAgeRule.Setup(t => t.Execute(app));

现在 t.Execute 返回一个“Rules”对象,我如何验证是否已在 Rules 上调用了某些内容?

我试图这样称呼它 mockValidAgeRule.Verify(x => x.Execute(app).Passed)

我想测试给定输入的对象 Result 返回 true 。

对于所有问题,我们深表歉意,只是我在查找有关最小起订量的最新且有帮助的信息时遇到了一些困难

Given the following:

var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
mockValidAgeRule.Setup(t => t.Execute(app));

Now t.Execute returns a "Rules" object, how can I verify that something has been called on Rules?

I am attempting to call it as such mockValidAgeRule.Verify(x => x.Execute(app).Passed)

I want to test that the object Result returned true given the inputs.

Sorry for all questions just am having a little trouble finding info about MOQ that is up to date and helpful

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

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

发布评论

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

评论(2

喜你已久 2024-10-01 08:10:42

好吧,正如克里斯所说,您没有提供足够的细节来获得正确的答案。尽管如此,恕我直言,很明显这个测试有代码味道。它似乎没有任何具体的实现。完全由模拟对象组成的测试不太可能测试任何有用的东西。

哪个类代表您的 SUT?听起来它可能是您的规则对象。如果您提供有关对象模型和预期行为的更多详细信息,则提供反馈会更容易。

Well, as stated by Chris, you haven't provided enough detail to get a proper answer. Nonetheless, IMHO it's pretty clear that this test has a code smell. It don't appear to have any concrete implementations. A test completely composed of mock objects is not likely testing anything useful.

Which class represents your SUT? It sounds like it might be your Rules object. If you provide further detail about your object model and expected behaviors, it would be easier to provide a feedback.

浸婚纱 2024-10-01 08:10:42

我建议添加 RulesMock 对象,如下所示:

var rulesMock = new Mock<Rules>();
rulesMock.SetUp(x => x.MethodInRules).Return(some_object);

然后将此模拟添加到您的代码中:

var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
mockValidAgeRule.Setup(t => t.Execute(app)).Returns(rulesMock.Object);

因此,如果您的 Rules getter 调用 MethodInRules(),您可以检查它的调用方式:

rulesMock.Verify(x => x.MethodInRules, Times.Once);

这是一个想法,希望这对某人有帮助,祝您好运!

I suggest to add RulesMock object something like this:

var rulesMock = new Mock<Rules>();
rulesMock.SetUp(x => x.MethodInRules).Return(some_object);

then add this mock to your code:

var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
mockValidAgeRule.Setup(t => t.Execute(app)).Returns(rulesMock.Object);

so, if your Rules getter call MethodInRules() you can check this is called by:

rulesMock.Verify(x => x.MethodInRules, Times.Once);

It is an idea, hope this helps someone, good luck!

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