5

I have an interface with a property:

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

I have a method simelar to this one:

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. I tried to do it in the following way, but it just tests the 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?

scher
  • 1,813
  • 2
  • 18
  • 39

1 Answers1

10

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:

filterable.Received().Filter = filter;
David Tchepak
  • 9,826
  • 2
  • 56
  • 68