NSubstitute intercepts setter call for property only

With NSubstitute, is there a way to capture the value that you pass to the property device?

eg. if I have the following interface:

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

Let's say I have a replacement created, for example:

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

Is there any way to intercept and fix the value if any of the โ€œestablishedโ€ replacement methods are called?

+7
source share
1 answer

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

If your interface should have properties for configuration purposes only, you can write 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); } 
+13
source

All Articles