简体   繁体   English

Nsubstitute检查是否已经调用了setter

[英]Nsubstitute check if setter has been called

I have an interface with a property: 我有一个属性接口:

public interface Filterable<T>
{
    Filter<T> Filter { get; set; }
}

I have a method simelar to this one: 我有一个方法simelar到这个:

public void SetTheFilter<T>(Filterable<T> filterable, Filter<T> filter)
{
    If (filter.IsActive)
        filterable.Filter = filter;
}

How can I ensure with NSubstitute with in a unit test that the filter has been set. 如何在单元测试中确保NSubstitute已设置过滤器。 I tried to do it in the following way, but it just tests the getter: 我试着用以下方式做到这一点,但它只测试了getter:

[TestMethod]
public void SetTheFilter_WhenCalledWithFilterActive_SetsTheFilterOfFilterable()
{
    var filterable = Substitute.For<Filterable<String>>();
    var filter = new StringFilter();

    SetTheFilter(filterable, filter);

    var tmp = filterable.Recieved().Filter;
}

Does anybody knows how to test, if the setter has been called? 如果已经调用了setter,有人知道如何测试吗?

The standard way to test this is to just read the value back out of the property: 测试这个的标准方法是从属性中读取值:

Assert.AreEqual(filterable.Filter, filter);

If for some reason you really want to test a setter was called, you can do: 如果由于某种原因你真的想测试一个setter被调用,你可以这样做:

filterable.Received().Filter = filter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM