使用最小起订量验证具有不同参数的单独调用

发布于 2024-10-11 07:35:37 字数 873 浏览 1 评论 0原文

我试图验证传递给后续模拟方法调用(同一方法)的参数值,但无法找出有效的方法。一个通用示例如下:

public class Foo
{
    [Dependency]
    public Bar SomeBar
    {
        get;
        set;
    }

    public void SomeMethod()
    {
        this.SomeBar.SomeOtherMethod("baz");
        this.SomeBar.SomeOtherMethod("bag");
    }
}

public class Bar
{
    public void SomeOtherMethod(string input)
    {
    } 
}

public class MoqTest
{
    [TestMethod]
    public void RunTest()
    {
        Mock<Bar> mock = new Mock<Bar>();
        Foo f = new Foo();
        mock.Setup(m => m.SomeOtherMethod(It.Is<string>("baz")));
        mock.Setup(m => m.SomeOtherMethod(It.Is<string>("bag"))); // this of course overrides the first call
        f.SomeMethod();
        mock.VerifyAll();
    }
}

在设置中使用函数可能是一个选项,但似乎我会被简化为某种全局变量来知道我正在验证哪个参数/迭代。也许我忽略了起订量框架中显而易见的内容?

I'm trying to validate the values of arguments passed to subsequent mocked method invocations (of the same method), but cannot figure out a valid approach. A generic example follows:

public class Foo
{
    [Dependency]
    public Bar SomeBar
    {
        get;
        set;
    }

    public void SomeMethod()
    {
        this.SomeBar.SomeOtherMethod("baz");
        this.SomeBar.SomeOtherMethod("bag");
    }
}

public class Bar
{
    public void SomeOtherMethod(string input)
    {
    } 
}

public class MoqTest
{
    [TestMethod]
    public void RunTest()
    {
        Mock<Bar> mock = new Mock<Bar>();
        Foo f = new Foo();
        mock.Setup(m => m.SomeOtherMethod(It.Is<string>("baz")));
        mock.Setup(m => m.SomeOtherMethod(It.Is<string>("bag"))); // this of course overrides the first call
        f.SomeMethod();
        mock.VerifyAll();
    }
}

Using a Function in the Setup might be an option, but then it seems I'd be reduced to some sort of global variable to know which argument/iteration I'm verifying. Maybe I'm overlooking the obvious within the Moq framework?

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

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

发布评论

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

评论(1

他是夢罘是命 2024-10-18 07:35:37

并不是我完全错了或者白蚁太宽容了,
但更好的答案由以下代码演示:

public interface IA
    {
        int doA(int i);
    }
    public class B
    {
        private IA callee;
        public B(IA callee)
        {
            this.callee = callee;
        }
        public int doB(int i)
        {
            Console.WriteLine("B called with " + i);
            var res = callee.doA(i);
            Console.WriteLine("Delegate returned " + res);
            return res;
        }
    }
    [Test]
    public void TEST()
    {
        var mock = new Mock<IA>();
        mock.Setup(a => a.doA(It.IsInRange(-5, 100, Range.Exclusive))).Returns((int i) => i * i);
        var b = new B(mock.Object);
        for (int i = 0; i < 10; i++)
        {
            b.doB(i);
        }

        mock.Verify(a => a.doA(It.IsInRange(0, 4, Range.Inclusive)), Times.Exactly(5));
        mock.Verify(a => a.doA(It.IsInRange(5, 9, Range.Inclusive)), Times.Exactly(5));
        mock.Verify(a => a.doA(It.Is<int>(i => i < 0)), Times.Never());
        mock.Verify(a => a.doA(It.Is<int>(i => i > 9)), Times.Never());
        mock.Verify(a => a.doA(It.IsInRange(3, 7, Range.Inclusive)), Times.Exactly(5));

        // line below will fail
        // mock.Verify(a => a.doA(It.IsInRange(3, 7, Range.Inclusive)), Times.Exactly(7));
    }

这表明设置与验证完全分开。在某些情况下,这意味着参数匹配必须进行两次:(

Not that I was completely wrong or Termite too tolerant,
but the better answer is demonstrated by the following code:

public interface IA
    {
        int doA(int i);
    }
    public class B
    {
        private IA callee;
        public B(IA callee)
        {
            this.callee = callee;
        }
        public int doB(int i)
        {
            Console.WriteLine("B called with " + i);
            var res = callee.doA(i);
            Console.WriteLine("Delegate returned " + res);
            return res;
        }
    }
    [Test]
    public void TEST()
    {
        var mock = new Mock<IA>();
        mock.Setup(a => a.doA(It.IsInRange(-5, 100, Range.Exclusive))).Returns((int i) => i * i);
        var b = new B(mock.Object);
        for (int i = 0; i < 10; i++)
        {
            b.doB(i);
        }

        mock.Verify(a => a.doA(It.IsInRange(0, 4, Range.Inclusive)), Times.Exactly(5));
        mock.Verify(a => a.doA(It.IsInRange(5, 9, Range.Inclusive)), Times.Exactly(5));
        mock.Verify(a => a.doA(It.Is<int>(i => i < 0)), Times.Never());
        mock.Verify(a => a.doA(It.Is<int>(i => i > 9)), Times.Never());
        mock.Verify(a => a.doA(It.IsInRange(3, 7, Range.Inclusive)), Times.Exactly(5));

        // line below will fail
        // mock.Verify(a => a.doA(It.IsInRange(3, 7, Range.Inclusive)), Times.Exactly(7));
    }

This shows that the setup is completely separated from validation. In some cases it means that argument matching has to be done twice :(

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