我已经下载了最新的NSubstitute版本,1.1.0,2011年5月21日。在此版本之前,似乎NSub不支持输出参数。似乎已经做了一些工作来通过中间版本提供支持:NSub Google Group。
所以,我在试图让所有部分工作时遇到一些麻烦。我使用SystemWrapper来模拟DirectoryInfo
这是我的界面:
public interface INetworkPath
{
void SetPath(string NetworkPath);
bool TryGetDirectoryInfo(out IDirectoryInfoWrap DirectoryInfo);
}
......和测试:
public void SetNetworkPath_SetDirectoryInfo()
{
var netPath = Substitute.For<INetworkPath>();
netPath.SetPath("SomeNetworkPath");
IDirectoryInfoWrap DirectoryInfo;
netPath.TryGetDirectoryInfo(out DirectoryInfo)
.Returns(d => { // cannot convert lambda expression to type bool because it is not a delegate type
d[1] = Substitute.For<IDirectoryInfoWrap>(); // d[1] is read only
return true;
});
Assert.IsNotNull(DirectoryInfo);
}
有没有办法从INetworkPath接口模拟out参数?
更新
尝试以下操作:虽然它编译,但DirectoryInfo
返回null:
[Test]
public void SetNetworkPath_SetDirectoryInfo()
{
var netPath = Substitute.For<INetworkPath>();
netPath.SetPath("SomeNetworkPath");
IDirectoryInfoWrap DirectoryInfo;
netPath.TryGetDirectoryInfo(out DirectoryInfo)
.Returns(d => {
d = (CallInfo)Substitute.For<IDirectoryInfoWrap>();
return true;
});
Assert.IsNotNull(DirectoryInfo);
}