Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSubstitute intercepting "setter" only property invocation

With NSubstitute, is there any way to capture the value you pass to a property setter?

E.g. if I have the following interface:

public interface IStudent {
    int Id { set; }
    string Name { set; }
}

The say I have a substitute created e.g:

var _studentSub = Substitute.For<IStudent>();

Is there any way I can intercept and capture the value if any of the "set" methods of the substitute are invoked?

like image 464
bstack Avatar asked Oct 03 '11 15:10

bstack


1 Answers

The standard approach for NSubstitute is to have properties with getters and setters, as then properties on substitutes will work as expected (i.e. you'll get back what is set).

If your interface has to have setter-only properties you can capture the value of an individual property using Arg.Do:

[Test]
public void Setter() {
    var sub = Substitute.For<IStudent>();
    var name = "";
    sub.Name = Arg.Do<string>(x => name = x);

    sub.Name = "Jane";

    Assert.AreEqual("Jane", name);
}
like image 104
David Tchepak Avatar answered Sep 27 '22 23:09

David Tchepak