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
How can I assert that a particular method was called using NUnit?
提问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.
您必须使用一些模拟框架,例如Typemock或Rhino Mocks或NMocks2。
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.File
doesn't have an interface, then probably you can't do. You have to wrap your call to System.IO.File
inside 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 ImplClass
which uses the interface Finder
and you want to make sure the Search
function 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());