Rhino Mocks AAAQuick Reference
Rhino Mocks AAAQuick Reference
Rhino Mocks AAAQuick Reference
Stub
IFoo stub = MockRepository. GenerateStub<IFoo>();
Mock or Stub
IFoo foo = MockRepository. GenerateMock<IFoo>();
or
IFoo foo = MockRepository. GenerateStub<IFoo>();
Method Call
Assert
foo.AssertWasCalled(x=>x.Do());
Property Getter
Arrange Act Assert
mock.Expect(x => x.Name).Return("Bob"); mock.VerifyAllExpectations(); mock.Stub(x => x.Name).Return("Bob"); mock.AssertWasCalled( x => { var ignored = x.Name; });
Property Setter
Arrange
mock.Expect(x => x.Name = "Bob");
or
mock.Expect( x => x.Name = Arg.Is("Bob"));
or
mock.Expect( x => x.Name = Arg<string>.Is.NotNull);
Act Assert
mock.VerifyAllExpectations();
or
mock.AssertWasCalled( x => x.Name = Arg.Is("Bob"));
or
mock.AssertWasCalled( x => x.Name = Arg<string>.Is.NotNull);
Repetition
Arrange Act Assert
mock.Expect(x => x.Do()).Repeat.Once(); mock.VerifyAllExpectations(); foo.AssertWasCalled(x => x.Do(), y => y.Repeat.Once());
y => y.Repeat.Once() y => y.Repeat.AtLeastOnce() y => y.Repeat.Twice() y => y.Repeat.Times(2) y => y.Repeat.Times(1,3)
Never
Arrange Act Assert
mock.Expect(x => x.Do()).Repeat.Never(); mock.VerifyAllExpectations(); foo.AssertWasNotCalled(x => x.Do());
Equal Constraint
plain short long
foo.AssertWasCalled(x => x.Do("Bob")); foo.AssertWasCalled(x => x.Do(Arg.Is("Bob"))); foo.AssertWasCalled(x => x.Do(Arg<string>.Is.Equal("Bob")));
Act Assert
mock.VerifyAllExpectations();
Constraints
Is equal? Is not equal? Is same? Is not same? Is null? Is greater than 1? Is greater than or equal 2? Is less than 3? Is less than or equal 2? Is argument of type SpecialPoint? Is Joe element of the collection? Is Sam the third element (index 2) of the collection? Has the collection three elements? Has the collection exactly the elements Bob, Joe and Sam in this order? Contains the collection the elements Joe, Sam and Bob in an arbitrary order?
Arg<Point>.Is.Equal(new Point(1, 2)) Arg<Point>.Is.NotEqual(new Point(2, 2)) Arg<Point>.Is.Same(point) Arg<Point>.Is.NotSame(new Point(1, 2)) Arg<string>.Is.Null Arg<int>.Is.GreaterThan(1) Arg<int>.Is.GreaterThanOrEqual(2) Arg<int>.Is.LessThan(3) Arg<int>.Is.LessThanOrEqual(2) Arg<SpecialPoint>.Is.TypeOf
Arg<ICollection<int>>.List.IsIn("Joe")
Arg<ICollection<int>>.List.Element(2, Is.Equal("Sam"))
Arg<ICollection<int>>.List.Count(Is.Equal(3))
Ignore Arguments
Is.Anything Is.Anything IgnoreArguments() IgnoreArguments()
mock.Expect( x => x.Do(Arg<string>.Is.Anything, Arg<int>.Is.Anything)); foo.AssertWasCalled( x => x.Do(Arg<string>.Is.Anything, Arg<int>.Is.Anything)); mock.Expect( x => x.Do("bye", 2)).IgnoreArguments(); foo.AssertWasCalled( x => x.Do("bye", 2),y=>y.IgnoreArguments());
Complex Constraints
Ensure that Do(arg) was called with arg starting with a B or a C
foo.AssertWasCalled(x => x.Do(Arg<string>.Matches( y => y.StartsWith("B", StringComparison.InvariantCulture) || y.StartsWith("C", StringComparison.InvariantCulture))));
Throwing Exceptions
Arrange
foo.Stub(x => x.Do()).Throw(new IOException());
Event Registration
Ensure that some method was registered for the event:
event Action Done foo.AssertWasCalled(x => x.Done += Arg<Action>.Is.Anything);