2 years ago

#65952

test-img

Robert25

NSubstitute Mocking new T property

I try to create a mock for an existing interface. When I try to read the value, I get a null reference exception

    public interface MyInterface<T> : MyInterface
    {
        new T Value { get; set; }
    }

    public interface MyInterface : MyReadonlyInterface
    {
        new object Value { get; set; }
    }

    public interface MyReadonlyInterface
    {
        object Value { get; }
    }
...
    var i = Substitute.For<MyInterface<bool>>();
    variable.Value = false;

Just after inititialize I see the value as "false". Later in the test, the value will become NULL. The usualy used

variable.Value.Returns(false)

also shows NULL. How do I have to use NSubstitute to get the required "false" without changing the interface?

c#

unit-testing

nsubstitute

0 Answers

Your Answer

Accepted video resources