我有一个预先存在的界面......
public interface ISomeInterface
{
void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)
并且我使用mixin扩展了这个表面...
public static class SomeInterfaceExtensions
{
public static void AnotherMethod(this ISomeInterface someInterface)
{
// Implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个叫这个我要测试的课程...
public class Caller
{
private readonly ISomeInterface someInterface;
public Caller(ISomeInterface someInterface)
{
this.someInterface = someInterface;
}
public void Main()
{
someInterface.AnotherMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
和测试,我想模拟界面并验证对扩展方法的调用...
[Test]
public void Main_BasicCall_CallsAnotherMethod()
{
// Arrange
var someInterfaceMock = new Mock<ISomeInterface>();
someInterfaceMock.Setup(x => x.AnotherMethod()).Verifiable();
var caller = new Caller(someInterfaceMock.Object);
// Act
caller.Main();
// Assert
someInterfaceMock.Verify();
}
Run Code Online (Sandbox Code Playgroud)
然而,运行此测试会产生异常......
System.ArgumentException: …Run Code Online (Sandbox Code Playgroud) 我使用IConfigurationRoute来访问这样的目录.
if (type == "error") directory = _config.GetValue<string>("Directories:SomeDirectory");
Run Code Online (Sandbox Code Playgroud)
_config是在构造函数中注入的IConfigurationRoot.
我尝试了以下方式来模拟它.
var mockConfigurationRoot = new Mock<IConfigurationRoot>();
mockConfigurationRoot.Setup(c => c.GetValue<string>("Directories: SomeDirectory"))
.Returns("SomeDirectory")
.Verifiable();
var config = mockConfigurationRoot.Object;
Run Code Online (Sandbox Code Playgroud)
问题是在运行测试时,Xunit会抛出异常说法
"System.NotSupportedException:Expression引用一个不属于模拟对象的方法"
我该如何解决这个问题?