简体   繁体   English

为什么全世界的Moq + NUnit测试都失败了?

[英]Why in the world is this Moq + NUnit test failing?

I have this dataAccess mock object and I'm trying to verify that one of its methods is being invoked, and that the argument passed into this method fulfills certain constraints. 我有这个dataAccess模拟对象,并且我试图验证其方法之一正在被调用,并且传递给此方法的参数满足某些约束。 As best I can tell, this method is indeed being invoked, and with the constraints fulfilled. 据我所知,此方法确实已被调用,并且满足了约束条件。 This line of the test throws a MockException: 该行测试抛出MockException:

data.Verify(d => d.InsertInvoice(It.Is<Invoice>(i => i.TermPaymentAmount == 0m)), Times.Once());

However, removing the constraint and accepting any invoice passes the test: 但是,删除约束并接受任何发票都可以通过测试:

data.Verify(d => d.InsertInvoice(It.IsAny<Invoice>()), Times.Once());

I've created a test windows form that instantiates this test class, runs its .Setup() method, and then calls the method which I am wishing to test. 我创建了一个测试Windows窗体,该窗体实例化该测试类,运行其.Setup()方法,然后调用我要测试的方法。 I insert a breakpoint on the line of code where the mock object is failing the test 我在模拟对象未通过测试的代码行上插入断点

data.InsertInvoice(invoice);

to actually hover over the invoice, and I can confirm that its .TermPaymentAmount decimal property is indeed zero at the time the method is invoked. 实际将鼠标悬停在发票上,我可以确认在调用该方法时其.TermPaymentAmount十进制属性确实为零。

Out of desperation, I even added a call back to my dataAccess mock: 出于绝望,我什至向我的dataAccess模拟添加了回叫:

data.Setup(d => d.InsertInvoice(It.IsAny<Invoice>())).Callback((Invoice inv) => MessageBox.Show(inv.TermPaymentAmount.ToString("G17")));

And this gives me a message box showing 0 . 这给了我一个显示0的消息框。 This is really baffling me, and no one else in my shop has been able to figure this out. 这真的让我感到困惑,而且我的商店中没有其他人能够弄清楚这一点。 Any help would be appreciated. 任何帮助,将不胜感激。

A barely related question, which I should probably ask independently, is whether it is preferable to use Mock.Verify() as I have here, or to use Mock.Expect() . 我可能应该独立询问一个几乎不相关的问题,是更可取的是使用Mock.Verify()还是使用Mock.Expect() Verifiable followed by Mock.VerifyAll() as I have seen other people doing? 我已经看到其他人在做Mock.VerifyAll()紧随其后的Mock.VerifyAll()吗? If the answer is situational, which situations would warrent the use of one over the other? 如果答案是根据情况而定,那么哪种情况会警告使用一种而不是另一种?

Just to rule this out (because I've got some strange behavior like you : 只是为了排除这种情况(因为我有一些像您这样的奇怪行为:
Try Times.Exactly(1) instead of Times.Once(). 尝试使用Times.Exactly(1)而不是Times.Once()。 I've had some weird thing happening when using Times.Once() Though, I would have guessed that Once() is using Exactly(1) internally... 使用Times.Once()时,我发生了一些奇怪的事情,不过,我会猜到Once()在内部使用Exactly(1)...

I am using version 3.1.0.0 of Moq and am not experiencing this issue with the following test case. 我正在使用Moq的3.1.0.0版本,并且在以下测试案例中未遇到此问题。

internal class Program
{
    private static void Main(string[] args)
    {
        var mock = new Mock<IData>();
        mock.Setup(d => d.InsertInvoice(It.IsAny<Invoice>()));

        var service = new MyService(mock.Object);
        service.DoSomething(new Invoice());

        mock.Verify(d => d.InsertInvoice(It.Is<Invoice>(i => i.TermPaymentAmount == 0m)), Times.Once());

        Console.ReadLine();
    }
}

internal class MyService
{
    private readonly IData _data;

    public MyService(IData data)
    {
        _data = data;
    }

    public void DoSomething(Invoice invoice)
    {
        _data.InsertInvoice(invoice);
    }
}

public class Invoice
{
    public decimal TermPaymentAmount { get; set; }
}

public interface IData
{
    void InsertInvoice(Invoice invoice);
}

Could you supply maybe a bit more information on how you are using the mock? 您能否提供更多有关如何使用模拟程序的信息?

My guess is that it may have something to do with the problems inherent with equivalency checking of floating point values. 我的猜测是,它可能与浮点值的等效检查所固有的问题有关。 Is your Invoice.TermPaymentAmount property value the result of a calculation, perhaps? 您的Invoice.TermPaymentAmount属性值是计算结果吗?

For an idea of what I'm talking about, see this possibly related question: Is it safe to check floating point values for equality to 0? 有关我在说什么的想法,请参见以下可能相关的问题: 检查浮点值是否等于0是否安全?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM