R – How to test a writeonly property

rhino-mocks

In my application, using MVP pattern, presenter is setting some properties on view.For example, Iview has string Customer {set;}.Now, I want to test that this property was set with some value "x".How can I do the test with rhino mocks?

Best Answer

Don't define write-only properties. As the .NET design guidelines say:

Do not provide set-only properties.

If the property getter cannot be provided, use a method to implement the functionality instead. The method name should begin with Set followed by what would have been the property name. For example, AppDomain has a method called SetCachePath instead of having a set-only property called CachePath.

In most cases, defining a read/write property is much easier, and it makes it a breeze to unit testi the owning type. You wouldn't need Rhino Mocks for that, as you can simply read the value directly from the property.

However, if you rather want a mutating method as described in the design guidelines, you must make it virtual to be able to use Rhino Mocks to verify that it was correctly called. Although this is certainly possible, it's more complicated to set up, so I would only take that route if there were compelling reasons to do so.

Related Topic