Hibernating Rhinos | 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.

   1: /// <summary>
   2: /// Asserting that an instance inside your system under test is called with a parameter
   3: /// matching a set of expected criteria. This example shows that our FooService should
   4: /// call it's dependency's method with the Name property of the Foo object passed into
   5: /// the LookUpFoo method.
   6: /// </summary>
   7: [Test]
   8: public void How_to_Assert_that_a_method_calls_an_expected_method_with_value()
   9: {
  10:     // Arrange
  11:     var foo = new Foo {Name = "rhino-mocks"};
  12:     var mockFooRepository = MockRepository.GenerateStub<IFooRepository>();
  13:     var fooService = new FooService(mockFooRepository);
  14:  
  15:     // Act
  16:     fooService.LookUpFoo(foo);
  17:  
  18:     // Assert
  19:     mockFooRepository.AssertWasCalled(x => x.GetFooByName(Arg<string>.Matches(y => y.Equals(foo.Name))));
  20: }
Last update: 12/19/2009 9:10:19 PM