如何模拟“出”参数?参数

2023-09-03 17:17:27 作者:假仙女真汉子

我已经下载了最新NSubstitute版本,1.1.0,5月21日,2011年在此版本之前,似乎NSUB不支持输出参数。看来,有些工作已经完成,通过一个中间版本提供支持: NSUB谷歌集团。

所以,我有一个小麻烦试图让所有的工作件。我使用 SystemWrapper 嘲笑的DirectoryInfo

下面是我的接口:

 公共接口INetworkPath
    {
        无效的setpath(字符串NetworkPath);
        布尔TryGetDirectoryInfo(出IDirectoryInfoWrap DirectoryInfo的);
    }
 

...和测试

 公共无效SetNetworkPath_SetDirectoryInfo()
{
    变种NETPATH​​ = Substitute.For&其中; INetworkPath>();
    netPath.SetPath(SomeNetworkPath);
    IDirectoryInfoWrap DirectoryInfo的;

    netPath.TryGetDirectoryInfo(出DirectoryInfo的)
           .Returns(D => {//不能将拉姆达EX pression为bool类型,因为它不是一个委托类型
               D [1] = Substitute.For&其中; IDirectoryInfoWrap>(); // D [1]是只读
               返回true;
           });

    Assert.IsNotNull(DirectoryInfo的);
}
 
中控DCS模拟量输出块参数设置中的信号类型 Ⅱ Ⅲ型是什么意思

有没有一种方法来模拟从INetworkPath接口的输出参数?

更新

试过如下:虽然编译,的DirectoryInfo 返回null:

  [测试]
公共无效SetNetworkPath_SetDirectoryInfo()
{
    变种NETPATH​​ = Substitute.For&其中; INetworkPath>();
    netPath.SetPath(SomeNetworkPath);
    IDirectoryInfoWrap DirectoryInfo的;

    netPath.TryGetDirectoryInfo(出DirectoryInfo的)
           .Returns(D => {
               D =(CallInfo)Substitute.For< IDirectoryInfoWrap>();
               返回true;
           });

    Assert.IsNotNull(DirectoryInfo的);
}
 

解决方案

我不相信你正在寻找被释放1.1,但完成之后实施(参考,并全力支持提交)。你可能会做的code一拉,并建立它自己。

I have downloaded the latest NSubstitute release, 1.1.0, May 21, 2011. Prior to this release, it seems that NSub did not support out parameters. It appears that some work has been done to provide support through an intermediate release: NSub Google Group.

So, I am having a little trouble trying to get all the pieces working. I am using SystemWrapper to mock DirectoryInfo

Here is my interface:

    public interface INetworkPath
    {
        void SetPath(string NetworkPath);
        bool TryGetDirectoryInfo(out IDirectoryInfoWrap DirectoryInfo);
    }

...and the test:

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);
}

Is there a way to mock the out parameter from the INetworkPath interface?

Update

Tried the following: although it compiles, DirectoryInfo returns 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);
}

解决方案

I don't believe the implementation you are looking for was released with 1.1 but done afterwards (Ref and out support commit). You will probably have to do a pull of the code and build it yourself.