Вы находитесь на странице: 1из 4

Rhino Mocks AAA Syntax Quick Reference

Create Mocks / Stubs


Mock
IFoo mock = MockRepository. GenerateMock<IFoo>();

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());

Method Call with Return Value


Arrange Act Assert
mock.Expect(x => x.Age()).Return(9); mock.VerifyAllExpectations(); foo.Stub(x => x.Age()).Return(9); foo.AssertWasCalled(x=>x.Age());

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();

mock.AssertWasCalled( x => x.Name = "Bob");

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());

Once At least once Twice Exactly 2 times Between 1 and 3 times

.Repeat.Once(); .Repeat.AtLeastOnce(); .Repeat.Twice(); .Repeat.Times(2); .Repeat.Times(1,3);

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());

Multiple Different Return Values


Arrange Act / Assert
foo.Stub(x => x.Age()).Return(20).Repeat.Once(); foo.Stub(x => x.Age()).Return(25).Repeat.Twice(); Assert.AreEqual(20, foo.Age()); Assert.AreEqual(25, foo.Age()); Assert.AreEqual(25, foo.Age());

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")));

Constraints in Expect() and Stub()


Arrange
mock.Expect(x => x.Calc(Arg.Is("Bob"))). Return(1); mock.Expect(x => x.Calc(Arg.Is("Joe"))). Return(2); foo.Stub(x => x.Calc(Arg.Is("Bob"))). Return(1); foo.Stub(x => x.Calc(Arg.Is("Joe"))). Return(2); foo.AssertWasCalled( x => x.Calc(Arg.Is("Bob"))); foo.AssertWasCalled( x => x.Calc(Arg.Is("Joe")));

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))

Arg<ICollection<int>>.List.Equal(new string[] { "Bob", "Joe", "Sam" })

Arg<ICollection<int>>.List.ContainsAll(new string[] { "Joe", "Sam", "Bob" })

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());

Plain Equal Constraint Limitation


Two plain constraints are ok Cant mix plain with others Cant mix plain with others
foo.AssertWasCalled(x => x.Do("hello", 2)); foo.AssertWasCalled( x => x.Do(Arg.Is("hello"), Arg<int>.Is.GreaterThan(1))); foo.AssertWasCalled( x => x.Do(Arg<string>.Is.Anything, Arg.Is(2)));

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);

Raise EventHandler Events


Instruct the mock/stub to raise the event:
event EventHandler Ready foo.Raise(x => x.Ready += null, foo, EventArgs.Empty);

Raise Other Events


Instruct the mock/stub to raise the event:
event Action<int,int> Finished const int arg1 = 2; const int arg2 = 4; foo.Raise(x => x.Finished += null, arg1, arg2);

Mock with Property Behavior


mock.Stub(x => x.Name).PropertyBehavior(); mock.Name = "Bob"; Assert.AreEqual("Bob", mock.Name);

Mocks/Stubs with Behavior


foo.Stub(x => x.Transform(Arg<string>.Is.Anything)).Return(null). WhenCalled(x => x.ReturnValue = ((string)(x.Arguments[0])).ToUpper()); Assert.AreEqual("BOB", foo.Transform("Bob"));

Return Out and Ref Parameters


Out parameter Ref parameter
foo.Expect(x => x.Do(out Arg<int>.Out(3).Dummy)); foo.Expect(x => x.DoRef(ref Arg<int>.Ref(Is.Anything(), 3).Dummy));

Verify Order of Expectations (pre AAA syntax required)


IFile fileMock = MockRepository.GenerateMock<IFile>(); using (fileMock.GetMockRepository().Ordered()) { // Copy() should be called before Delete() fileMock.Expect(x => x.Copy(Arg.Is("test.txt"), Arg<string>.Is.Anything)); fileMock.Expect(x => x.Delete(Arg.Is("test.txt"))); } using (fileMock.GetMockRepository().Playback()) { // Act }

Вам также может понравиться