【发布时间】:2019-07-12 12:44:17
【问题描述】:
我正在尝试使用Arg.Any<T>() 参数模拟该方法。模拟方法在调用 .netframework 而不是 netcoreapp 时正确重定向。
这是一个与此问题类似的link,但此问题似乎是 NSubstitute 在网络核心中无法正常工作。
Github 链接是here
注意:不是传递 Arg.Any 值,而是传递特定的参数值,它可以正常工作
在 netframework(4.5.1) 和 net core app(2.1) 库中进行比较。
Dotnet Core 结果:
Test Name: ClassLibrary1.Class1.Method
Test FullName: ClassLibrary1.Class1.Method
Test Source: C:\Users\mmohan\source\repos\ClassLibrary1\ClassLibrary1\Class1.cs : line 10
Test Outcome: Failed
Test Duration: 0:00:00.177
Result StackTrace: at ClassLibrary1.Class1.Method() in C:\Users\mmohan\source\repos\ClassLibrary1\ClassLibrary1\Class1.cs:line 16
Result Message:
Assert.Throws() Failure
Expected: typeof(System.FormatException)
Actual: (No exception was thrown)
净框架结果:
Test Name: ClassLibrary1.Class1.Method
Test FullName: ClassLibrary1.Class1.Method
Test Source: C:\Users\mmohan\source\repos\SampleTestIssue\ClassLibrary1\Class1.cs : line 10
Test Outcome: Passed
Test Duration: 0:00:00.292
我的代码:
using NSubstitute;
using System;
using Xunit;
namespace ClassLibrary1
{
public class Class1
{
[Fact]
public void Method()
{
IB b = Substitute.For<IB>();
A a = new A(b);
b.doSomeB(Arg.Any<string>()).Returns(x => { throw new FormatException("Something there"); });
a.doSomeA("");
Exception exception = Assert.Throws<FormatException>(
() => b.doSomeB(Arg.Any<string>()));
}
}
public class A
{
public IB _b;
public A(IB b)
{_b = b;}
public void doSomeA(string aa)
{
try
{ _b.doSomeB("");}
catch (Exception ex){ }
}
}
public class B : IB
{
public string doSomeB(string bb)
{
try{ }
catch (Exception ex){ }
return "";
}
}
public interface IB
{
string doSomeB(string bb);
}
}
【问题讨论】:
-
我认为您可以将此作为问题发布到 nsub github。
标签: c# .net-core nsubstitute