C# 如何断言使用 NUnit 调用了特定方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1837933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:17:11  来源:igfitidea点击:

How can I assert that a particular method was called using NUnit?

c#tddnunitmoq

提问by Umair Ahmed

How can I test that a particular method was called with the right parameters as a result of a test? I am using NUnit.

作为测试的结果,如何测试使用正确的参数调用了特定方法?我正在使用NUnit

The method doesn't return anything. it just writes on a file. I am using a mock object for System.IO.File. So I want to test that the function was called or not.

该方法不返回任何内容。它只是写在一个文件上。我正在为System.IO.File. 所以我想测试该函数是否被调用。

采纳答案by Dmytrii Nagirniak

More context is needed. So I'll put one here adding Moq to the mix:

需要更多的上下文。所以我会放一个在这里添加 Moq 到组合中:

pubilc class Calc {
    public int DoubleIt(string a) {
        return ToInt(a)*2;
    }

    public virtual int ToInt(string s) {
        return int.Parse(s);
    }
}

// The test:
var mock = new Mock<Calc>();
string parameterPassed = null;
mock.Setup(c => x.ToInt(It.Is.Any<int>())).Returns(3).Callback(s => parameterPassed = s);

mock.Object.DoubleIt("3");
Assert.AreEqual("3", parameterPassed);

回答by Graviton

You have to use some mocking framework, such as Typemockor Rhino Mocks, or NMocks2.

您必须使用一些模拟框架,例如TypemockRhino MocksNMocks2

NUnit also has a Nunit.Mock, but it is not well-known.

NUnit 也有一个Nunit.Mock,但它并不广为人知。

The syntax for moq can be found here:

moq 的语法可以在这里找到:

var mock = new Mock<ILoveThisFramework>();

// WOW! No record/reply weirdness?! :)
mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
    .Returns(true)
    .AtMostOnce();

// Hand mock.Object as a collaborator and exercise it, 
// like calling methods on it...
ILoveThisFramework lovable = mock.Object;
bool download = lovable.DownloadExists("2.0.0.0");

// Verify that the given method was indeed called with the expected value
mock.Verify(framework => framework.DownloadExists("2.0.0.0"));

Also, note that you can only mock interface, so if your object from System.IO.Filedoesn't have an interface, then probably you can't do. You have to wrap your call to System.IO.Fileinside your own custom class for the job.

另外,请注意,您只能模拟interface,因此如果您的对象 fromSystem.IO.File没有接口,那么您可能无法做到。您必须将您的呼叫包装System.IO.File在您自己的自定义类中以进行作业。

回答by tster

By using a mock for an interface.

通过对接口使用模拟。

Say you have your class ImplClasswhich uses the interface Finderand you want to make sure the Searchfunction gets called with the argument "hello";

假设您有ImplClass一个使用接口的类,Finder并且您想确保Search使用参数“hello”调用该函数;

so we have:

所以我们有:

public interface Finder 
{
  public string Search(string arg);
}

and

public class ImplClass
{
  public ImplClass(Finder finder)
  {
    ...
  }
  public void doStuff();
}

Then you can write a mock for your test code

然后你可以为你的测试代码编写一个模拟

private class FinderMock : Finder
{
  public int numTimesCalled = 0;
  string expected;
  public FinderMock(string expected)
  {
    this.expected = expected;
  }
  public string Search(string arg)
  {
    numTimesCalled++;
    Assert.AreEqual(expected, arg);
  }
}

then the test code:

然后是测试代码:

FinderMock mock = new FinderMock("hello");
ImplClass impl = new ImplClass(mock);
impl.doStuff();
Assert.AreEqual(1, mock.numTimesCalled);

回答by Hasse

In Rhino Mocks where is a method called AssertWasCalled

在 Rhino Mocks 中,有一个方法叫做 AssertWasCalled

Here is a way to use it

这是一种使用方法

var mailDeliveryManager = MockRepository.GenerateMock<IMailDeliveryManager>();

var mailHandler = new PlannedSending.Business.Handlers.MailHandler(mailDeliveryManager);

mailHandler.NotifyPrinting(User, Info);

mailDeliveryManager.AssertWasCalled(x => x.SendMailMessage(null, null, null), o => o.IgnoreArguments());