开发者

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 t开发者_如何学JAVAhe 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜