May I know if an object has been accessed via Rhino Mocks?
I have a property in the frontend that I mocked Rhino Mocks. I want to know if it was available in my unit test.
Is there a way to find out if a property was available via Rhino Mocks?
I found this code here , but it doesn't work:
string name = customerMock.Name;
customerMock.AssertWasCalled(x => {var ignored = x.Name;});
I have reproduced this code and I get this:
Rhino.Mocks.Exceptions.ExpectationViolationException: IAddAddressForm.get_FirstName(); Expected #1, Actual #0..
I would like to use this syntax, but I am missing something. I call this a stub (not a layout), does it matter?
source to share
It turns out the syntax I was using works. But it only works if the property is not in PropertyBehavior mode. (This is the default for stubs and can be enabled for mocks).
Once the PropertyBehavior is enabled then this syntax does not work. According to Ayende , you should check the value at this point.
source to share
Is there a way to see if a property has been accessed through Rhino Mocks?
Yes. You can customize the expected property. If the check is not thrown, you know that access to it was available:
var mocks = new MockRepository();
IFoo foo = mocks.DynamicMock<IFoo>;
// this sets up an expectation that Name is accessed, and what it should return
Expect.Call(foo.Name).Return("John Doe");
mocks.ReplayAll()
// test something that uses IFoo here ...
mocks.VerifyAll(); // Throws if foo.Name was not accessed after ReplayAll()
This is all assuming that you want to foo.Name
be available. If your goal is to check that it is not , you should simply use StrictMock instead of DynamicMock. The strict layout will throw any unexpected calls.
source to share
If you want to assert that a method was called, or a property is available on a fake object, you should always use mock. Stubs are for situations where you don't want to test for interoperability, but just want to make sure that a call to some object returns a specific value to make a logical flow.
source to share
I usually use Moq which Castle uses, so you might want to consider it instead of / along with Rhino Mocks. Here's how you can do it:
// expects an invocation to set the value to "foo"
mock.SetupSet(foo => foo.Name = "foo");
// or verify the setter directly
mock.VerifySet(foo => foo.Name = "foo");
source to share