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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With