4

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);
}
IAbstract
  • 19,551
  • 15
  • 98
  • 146
  • @Bronumski's answer is correct; support for this is unreleased as of July 2011. We're hoping to get a 1.2 release out soon with this change. – David Tchepak Jul 28 '11 at 23:49
  • thanks, @David ...looking forward to the next release. – IAbstract Jul 28 '11 at 23:51
  • 1
    FYI, NSubstitute 1.2 has been released with basic support for [setting out and ref parameters](http://nsubstitute.github.com/help/setting-out-and-ref-arguments/). Apologies for the delay. – David Tchepak Sep 05 '11 at 13:29
  • Possible duplicate of [NSubstitute - mock out parameter behaviour for any parameter](https://stackoverflow.com/questions/51116836/nsubstitute-mock-out-parameter-behaviour-for-any-parameter) – Liam Aug 08 '19 at 13:51

1 Answers1

2

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.

Bronumski
  • 14,009
  • 6
  • 49
  • 77