如何处理 NSubstitute 中的委托参数
How to handle delegate parameter in NSubstitute
我有一个 class 方法,其签名如下:
public async Task<ResponseType> getSuff(string id,
string moreInfo,
deletegateName doStuff
) { // details. }
我正在尝试像这样使用 NSubstitute 模拟此调用:
MyClass helper = Substitute.ForPartsOf<MyClass>();
ResponseType response = new ResponseType();
helper.getSuff(Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<DelegateDefn>()).Returns(Task.FromResult(response));
但是我遇到运行时错误:
NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException:
Could not find a call to return from.
Make sure you called Returns() after calling your substitute (for
example: mySub.SomeMethod().Returns(value)), and that you are not
configuring other substitutes within Returns() (for example, avoid
this: mySub.SomeMethod().Returns(ConfigOtherSub())).
If you substituted for a class rather than an interface, check that
the call to your substitute was on a virtual/abstract member. Return
values cannot be configured for non-virtual/non-abstract members.
Correct use: mySub.SomeMethod().Returns(returnValue);
Potentially problematic use:
mySub.SomeMethod().Returns(ConfigOtherSub()); Instead try: var
returnValue = ConfigOtherSub();
mySub.SomeMethod().Returns(returnValue);
我认为问题出在代表身上。我只是想模拟方法,我不希望实际传递的委托被执行。
那么,有谁知道我怎样才能做到这一点?
谢谢
NSub 与委托和异步代码完美配合。这是一个简单的例子:
public delegate void SomeDelegate();
public class Foo
{
public virtual async Task<int> MethodWithDelegate(string something, SomeDelegate d)
{
return await Task.FromResult(1);
}
}
[Test]
public void DelegateInArgument()
{
var sub = Substitute.For<Foo>();
sub.MethodWithDelegate("1", Arg.Any<SomeDelegate>()).Returns(1);
Assert.AreEqual(1, sub.MethodWithDelegate("1", () => {}).Result);
Assert.AreNotEqual(1, sub.MethodWithDelegate("2", () => {}).Result);
}
请确保您指定的方法是虚拟的或抽象的。从你得到的异常:
If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member. Return values cannot be configured for non-virtual/non-abstract members.
我有一个 class 方法,其签名如下:
public async Task<ResponseType> getSuff(string id,
string moreInfo,
deletegateName doStuff
) { // details. }
我正在尝试像这样使用 NSubstitute 模拟此调用:
MyClass helper = Substitute.ForPartsOf<MyClass>();
ResponseType response = new ResponseType();
helper.getSuff(Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<DelegateDefn>()).Returns(Task.FromResult(response));
但是我遇到运行时错误:
NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException: Could not find a call to return from.
Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)), and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).
If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member. Return values cannot be configured for non-virtual/non-abstract members.
Correct use: mySub.SomeMethod().Returns(returnValue);
Potentially problematic use: mySub.SomeMethod().Returns(ConfigOtherSub()); Instead try: var returnValue = ConfigOtherSub(); mySub.SomeMethod().Returns(returnValue);
我认为问题出在代表身上。我只是想模拟方法,我不希望实际传递的委托被执行。
那么,有谁知道我怎样才能做到这一点? 谢谢
NSub 与委托和异步代码完美配合。这是一个简单的例子:
public delegate void SomeDelegate();
public class Foo
{
public virtual async Task<int> MethodWithDelegate(string something, SomeDelegate d)
{
return await Task.FromResult(1);
}
}
[Test]
public void DelegateInArgument()
{
var sub = Substitute.For<Foo>();
sub.MethodWithDelegate("1", Arg.Any<SomeDelegate>()).Returns(1);
Assert.AreEqual(1, sub.MethodWithDelegate("1", () => {}).Result);
Assert.AreNotEqual(1, sub.MethodWithDelegate("2", () => {}).Result);
}
请确保您指定的方法是虚拟的或抽象的。从你得到的异常:
If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member. Return values cannot be configured for non-virtual/non-abstract members.