Does NSubstitute support ref parameters?

I have the following method signature on my iteration:

void SetItem(ref AddressItem item);

I am doing a parameter restriction as follows:

IAddAddressForm form = Substitute.For<IAddAddressForm>();
AddressItem item = null;
form.SetItem(Arg.Is(item));

But this fails due to the link. If I take a waiver, then it works great. But I need to follow the link here.

Any ideas how to get this?

Side note. My ultimate goal is to throw an exception in SetItem if the passed value is null. If you can help with this, you will get extra points!

+5
source share
1 answer

NSubstitute does not have direct support for arg matching parameters, but overall it will work fine with them.

, ref, , , , ref, API , ( ).

ref, , :

form.SetItem(ref item);

, . , , , , . (, , , , .)

:

form.When(x => x.SetItem(ref item)).Do(x => { throw new ArgumentNullException(); });

null ref. , , , , , .

form.WhenForAnyArgs(x => x.SetItem(ref item))
    .Do(x => {
        if (x[0] == null)
            throw new ArgumentNullException();
    });

, , , , , IAddAddressForm arg, :

form
    .WhenForAnyArgs(x => x.SetItem(ref item))
    .Do(x => { throw new ArgumentNullException(); });

, , , , .

, .

:

arg (, Arg.Any<AddressItem>()) out ref, ( : , , ):

        IAddAddressForm form = Substitute.For<IAddAddressForm>();
        AddressItem item = Arg.Is<AddressItem>(y => y.Number == 14);
        form
            .When(x => x.SetItem(ref item))
            .Do(x => { throw new ArgumentNullException(); });
        var address = new AddressItem { Number = 14 };
        form.SetItem(ref address);
+10

All Articles