public void OrderedExecutionOfUnorderedSequence() { MockRepository mocks = new MockRepository(); ISongBird maleBird = (ISongBird)mocks.StrictMock(typeof(ISongBird)), femaleBird = (ISongBird)mocks.StrictMock(typeof(ISongBird)); 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(); femaleBird.Eat("seeds", 250); maleBird.Eat("seeds", 250); femaleBird.Mate(maleBird); maleBird.Mate(femaleBird); mocks.VerifyAll(); }
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(); }
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 ExampleUsingCallbacks() { MockRepository mocks = new MockRepository(); ISongBird maleBird = (ISongBird)mocks.StrictMock(typeof(ISongBird)), femaleBird = (ISongBird)mocks.StrictMock(typeof(ISongBird)); using (mocks.Ordered()) { using (mocks.Unordered()) { maleBird.MoveToCage(null); LastCall.On(maleBird).Callback(new CageDelegate(IsSameCage)); femaleBird.MoveToCage(null); LastCall.On(femaleBird).Callback(new CageDelegate(IsSameCage)); } maleBird.Eat("seeds", 250); femaleBird.Eat("seeds", 250); using (mocks.Unordered()) { maleBird.Mate(femaleBird); femaleBird.Mate(maleBird); } } mocks.ReplayAll(); BirdVeterinary vet = new BirdVeterinary(); vet.Mate(maleBird, femaleBird); mocks.VerifyAll(); }
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() { 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 Mate(ISongBird male, ISongBird female) { female.MoveToCage(cage); male.MoveToCage(cage); male.Eat("seeds", 250); female.Eat("seeds", 250); male.Mate(female); female.Mate(male); }
public void OrderedExecutionOfUnorderedSequence() { ISongBird maleBird = MockRepository.Mock <ISongBird>(); ISongBird femaleBird = MockRepository.Mock <ISongBird>(); 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)); femaleBird.Eat("seeds", 250); maleBird.Eat("seeds", 250); femaleBird.Mate(maleBird); maleBird.Mate(femaleBird); maleBird.VerifyAllExpectations(); femaleBird.VerifyAllExpectations(); }
public void OrderedExecutionOfUnorderedSequence() { ISongBird maleBird = MockRepository.Mock <ISongBird>(); maleBird.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault); ISongBird femaleBird = MockRepository.Mock <ISongBird>(); femaleBird.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault); 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)); femaleBird.Eat("seeds", 250); maleBird.Eat("seeds", 250); femaleBird.Mate(maleBird); maleBird.Mate(femaleBird); maleBird.VerifyAllExpectations(); femaleBird.VerifyAllExpectations(); }