public void SetupResultWithNestedOrdering() { ISongBird maleBird = MockRepository.Mock <ISongBird>(); maleBird.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault); ISongBird femaleBird = MockRepository.Mock <ISongBird>(); femaleBird.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault); maleBird.Stub(x => x.Sing()) .Return(""); maleBird.Expect(x => x.Eat("seeds", 250)); femaleBird.Expect(x => x.Eat("seeds", 250)); maleBird.Expect(x => x.Mate(femaleBird)); femaleBird.Expect(x => x.Mate(maleBird)); maleBird.Sing(); femaleBird.Eat("seeds", 250); maleBird.Sing(); maleBird.Eat("seeds", 250); maleBird.Sing(); femaleBird.Mate(maleBird); maleBird.Sing(); maleBird.Mate(femaleBird); maleBird.Sing(); maleBird.VerifyAllExpectations(); femaleBird.VerifyAllExpectations(); }
public void ExampleUsingParameterMatchingAndConstraints() { MockRepository mocks = new MockRepository(); ISongBird bird = (ISongBird)mocks.StrictMock(typeof(ISongBird)); bird.Eat("seeds", 500); //verifying expected values bird.Sing(); LastCall.On(bird).Return("Chirp, Chirp"); bird.Sing(); string exceptionMessage = "No food, no song"; LastCall.On(bird).Throw(new Exception(exceptionMessage)); mocks.ReplayAll(); bird.Eat("seeds", 500); Assert.Equal("Chirp, Chirp", bird.Sing()); try { bird.Sing(); Assert.False(true, "Exception expected"); } catch (Exception e) { Assert.Equal(exceptionMessage, e.Message); } mocks.VerifyAll(); }
public void ExampleUsingParameterMatchingAndConstraints() { ISongBird bird = MockRepository.Mock <ISongBird>(); bird.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault); bird.Expect(x => x.Eat("seeds", 500)); bird.Expect(x => x.Sing()) .Return("Chirp, Chirp"); bird.Expect(x => x.Sing()) .Throws(new Exception("No food, no song")); bird.Eat("seeds", 500); Assert.Equal("Chirp, Chirp", bird.Sing()); try { bird.Sing(); Assert.False(true, "Exception expected"); } catch (Exception e) { Assert.Equal("No food, no song", e.Message); } bird.VerifyAllExpectations(); }
public void SetupResultWithNestedOrdering() { MockRepository mocks = new MockRepository(); ISongBird maleBird = (ISongBird)mocks.StrictMock(typeof(ISongBird)), femaleBird = (ISongBird)mocks.StrictMock(typeof(ISongBird)); SetupResult.On(maleBird).Call(maleBird.Sing()).Return(""); using (mocks.Ordered()) { using (mocks.Unordered()) { maleBird.Eat("seeds", 250); femaleBird.Eat("seeds", 250); } using (mocks.Unordered()) { maleBird.Mate(femaleBird); femaleBird.Mate(maleBird); } } mocks.ReplayAll(); maleBird.Sing(); femaleBird.Eat("seeds", 250); maleBird.Sing(); maleBird.Eat("seeds", 250); maleBird.Sing(); femaleBird.Mate(maleBird); maleBird.Sing(); maleBird.Mate(femaleBird); maleBird.Sing(); mocks.VerifyAll(); }