Assert that a method is called with a value in an expected state

When you have a dependency that your system under test uses, you may want to ensure that it is called with a value in an expected state. The following example is looking up a "Foo" and we're going to ensure that we're calling the "FooRepository" with the Foo's Name property. /// /// Asserting that an instance inside your system under test is called with a parameter /// matching a set of expected criteria. This example shows that our FooService should /// call it's dependency's method with the Name property of the Foo object passed into /// the LookUpFoo method. /// [Test] public void How_to_Assert_that_a_method_calls_an_expected_method_with_value() { // Arrange var foo = new Foo {Name = "rhino-mocks"}; var mockFooRepository = MockRepository.GenerateStub(); var fooService = new FooService(mockFooRepository); // Act fooService.LookUpFoo(foo); // Assert mockFooRepository.AssertWasCalled(x => x.GetFooByName(Arg.Matches(y => y.Equals(foo.Name)))); }

Mocking a Read-Only Property

If you come into a situation where you need to mock a read-only property on a class, here is how to do so: /// /// When you need to mock a read-only property of a class. /// [Test] public void How_to_Stub_out_your_own_value_of_a_ReadOnlyProperty() { // Arrange var foo = MockRepository.GenerateStub(); foo.Stub(x => x.ID).Return(123); // Act var id = foo.ID; // Assert Assert.That(id, Is.EqualTo(123)); } This code is fairly simple and straightforward. Using MockRepository.GenerateStub, you'll mock an instance of IFoo, then have the ability to control what the return value is for a specific function. After the line declaring and setting the variable "foo", you'll see that I'm using the Stub extension method to pass a lambda expression of the property of which I want to control the return value. Now, my instance of IFoo will return 123 whenever the property ID is retrieved. This test is not testing any business logic, it just shows how to control the return value of a read-only property. For the entire code example, please check out the RhinoMocks.GettingStarted project. This example can be found in code [here](