使用 TypeMock 验证是否未调用具有特定参数的方法

发布于 2024-11-30 02:21:49 字数 511 浏览 2 评论 0原文

我正在使用 Typemock 进行一些单元测试。我模拟了静态类 Widget。我想模拟 Widget.GetPrice(123) 的返回以返回值 A。

Isolate.Fake.StaticMethods<Widget>();
Isolate.WhenCalled(() => Widget.GetPrice(123)).WillReturn("A");

我还想验证 Widget.GetPrice(456) 没有被调用。

Isolate.Verify.WasNotCalled(() => Widget.GetPrice(456));

WasNotCalled 似乎没有考虑参数。测试返回说它失败了,因为实际上调用了 Widget.GetPrice。

我想到的唯一方法是执行 DoInstead 调用并在调用 Widget.GetPrice(456) 时增加计数器。测试结束时将检查计数器是否增加。有没有更好的方法来做到这一点?

I am using Typemock for some of my unit tests. I have mocked static class Widget. I want to mock the return of Widget.GetPrice(123) to return value A.

Isolate.Fake.StaticMethods<Widget>();
Isolate.WhenCalled(() => Widget.GetPrice(123)).WillReturn("A");

I also want to verify that Widget.GetPrice(456) was NOT called.

Isolate.Verify.WasNotCalled(() => Widget.GetPrice(456));

It seems that WasNotCalled does not take parameters into consideration. The test comes back saying that it failed b/c Widget.GetPrice was in fact called.

The only way I could think to do this is to do a DoInstead call and increment a counter when Widget.GetPrice(456) is called. The end of the test would check if the counter was incremented. Is there a better way to do this though?

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

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

发布评论

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

评论(2

幸福还没到 2024-12-07 02:21:49

有几种方法可以做到这一点。

首先,你的 DoInstead 想法非常好,但我会调整它以包含一个断言:

Isolate.WhenCalled(() => Widget.GetPrice(0)).DoInstead(
  context =>
  {
    var num = (int)context.Parameters[0];
    Assert.AreNotEqual(456, num);
    return "A";
  });

这个想法是,当调用该方法时,你在调用时验证传入参数是否是你所期望的并且测试失败如果不是,则使用 Assert 语句。

(您还会注意到,我将“0”放入参数中,因为正如您所注意到的,实际值并不重要。我发现当参数不重要时,使用 null/0/etc 会更容易进行将来的维护这样您就不会“忘记”或“被愚弄”认为它确实很重要。)

您可以做的第二件事是使用 WithExactArguments 来控制行为。

// Provide some default behavior that will break things
// if the wrong value comes in.
Isolate
  .WhenCalled(() => Widget.GetPrice(0))
  .WillThrow(new InvalidOperationException());

// Now specify the right behavior if the arguments match.
Isolate
  .WhenCalled(() => Widget.GetPrice(123))
  .WithExactArguments()
  .WillReturn("A");

“WithExactArguments”将使您的行为在参数匹配时运行。当您运行测试时,如果传入无效值,将引发异常并且测试将失败。

无论哪种方式,您最终都会使用“WhenCalled”部分来处理您的断言,而不是“Verify”调用。

There are a couple of ways you can do this.

First, your DoInstead idea is pretty good, but I would tweak it to contain an assertion:

Isolate.WhenCalled(() => Widget.GetPrice(0)).DoInstead(
  context =>
  {
    var num = (int)context.Parameters[0];
    Assert.AreNotEqual(456, num);
    return "A";
  });

The idea there is that when the method is called, you verify at the time of the call that the incoming parameter is what you expect and fail the test with the Assert statement if it's not.

(You'll also note I put "0" in as the parameter because, as you noticed, the actual value doesn't matter. I find it's easier for future maintenance to use null/0/etc when the parameters don't matter so you don't "forget" or "get fooled" into thinking that it does matter.)

The second thing you could do is use WithExactArguments to control the behavior.

// Provide some default behavior that will break things
// if the wrong value comes in.
Isolate
  .WhenCalled(() => Widget.GetPrice(0))
  .WillThrow(new InvalidOperationException());

// Now specify the right behavior if the arguments match.
Isolate
  .WhenCalled(() => Widget.GetPrice(123))
  .WithExactArguments()
  .WillReturn("A");

The "WithExactArguments" will make your behavior run only if the arguments match. When you run your test, if an invalid value gets passed in, the exception will get thrown and the test will fail.

Either way, you end up using the "WhenCalled" part of things to handle your assertion rather than a "Verify" call.

彩虹直至黑白 2024-12-07 02:21:49

免责声明,我在 Typemock 工作。

由于我们的 API 正在不断改进,请查看针对您的问题的另一种解决方案。您可以

Isolate.WhenCalled((<type arg1>, <type arg1>) => fake<method> (<arg1>, <arg2>)).AndArgumentsMatch((<arg1>, <arg2>) => <check>.<behavior>;

用于设置您想要的参数的行为。

另外,不需要抛出任何异常。使用 DoInstead() 如下所示来验证未使用确切参数调用哪个方法。不需要内心的断言。

[TestMethod, Isolated]
public void TestWidget()
{
    Isolate.WhenCalled((int n) => Widget.GetPrice(n)).AndArgumentsMatch((n) => n == 123).WillReturn("A");

    bool wasCalledWith456 = false;

    Isolate.WhenCalled(() => Widget.GetPrice(456)).WithExactArguments().DoInstead((context) =>
    {
        wasCalledWith456 = true;
        context.WillCallOriginal();
        return null;
    });

    Assert.AreEqual("A", Widget.GetPrice(123));
    Widget.GetPrice(234);
    Widget.GetPrice(345);
    Widget.GetPrice(456);

    Assert.IsFalse(wasCalledWith456);
}

Disclaimer, I work in Typemock.

Since our API is constantly improving, check out another solution for your problem. You can use

Isolate.WhenCalled((<type arg1>, <type arg1>) => fake<method> (<arg1>, <arg2>)).AndArgumentsMatch((<arg1>, <arg2>) => <check>.<behavior>;

for setting your behavior for arguments you want.

Also, don't need to throw any exceptions. Use DoInstead() as shown below to verify, what method wasn't called with exact arguments. No inner assertion is needed.

[TestMethod, Isolated]
public void TestWidget()
{
    Isolate.WhenCalled((int n) => Widget.GetPrice(n)).AndArgumentsMatch((n) => n == 123).WillReturn("A");

    bool wasCalledWith456 = false;

    Isolate.WhenCalled(() => Widget.GetPrice(456)).WithExactArguments().DoInstead((context) =>
    {
        wasCalledWith456 = true;
        context.WillCallOriginal();
        return null;
    });

    Assert.AreEqual("A", Widget.GetPrice(123));
    Widget.GetPrice(234);
    Widget.GetPrice(345);
    Widget.GetPrice(456);

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