public void UsingTheWithMocksExceptingInSameOrderVerifyConstruct_ShouldTakeCareOfOrder() { MockRepository mocks = new MockRepository(); IDemo demo = mocks.StrictMock <IDemo>(); bool verificationFailed; try { With.Mocks(mocks).ExpectingInSameOrder(delegate { Expect.Call(demo.ReturnIntNoArgs()).Return(1); Expect.Call(demo.ReturnStringNoArgs()).Return("2"); }) .Verify(delegate { demo.ReturnStringNoArgs(); demo.ReturnIntNoArgs(); }); verificationFailed = false; } catch (ExpectationViolationException) { verificationFailed = true; } Assert.True(verificationFailed, "Verification was supposed to fail, because the mocks are called in the wrong order"); }
public void LastCallReturn() { demo.ReturnIntNoArgs(); LastCall.Return(5); mocks.ReplayAll(); Assert.Equal(5, demo.ReturnIntNoArgs()); }
public void SetReturnValueOnMethodWithCallback() { demo.ReturnIntNoArgs(); LastCall.On(demo).Callback(NoArgsMethod).Return(5); mocks.Replay(demo); Assert.Equal(5, demo.ReturnIntNoArgs()); mocks.Verify(demo); }
public void SetReturnValueOnMethodWithCallback() { demo.Expect(x => x.ReturnIntNoArgs()) .WhenCalled(() => NoArgsMethod()) .Return(5); Assert.Equal(5, demo.ReturnIntNoArgs()); demo.VerifyExpectations(true); }
public void UsingTheWithMocksConstruct() { With.Mocks(new MockRepository(), delegate { IDemo demo = Mocker.Current.StrictMock <IDemo>(); Expect.Call(demo.ReturnIntNoArgs()).Return(5); Mocker.Current.ReplayAll(); Assert.Equal(5, demo.ReturnIntNoArgs()); }); }
public void UsingTheWithMocksExceptingVerifyConstruct() { MockRepository mocks = new MockRepository(); IDemo demo = mocks.StrictMock <IDemo>(); With.Mocks(mocks) .Expecting(delegate { Expect.Call(demo.ReturnIntNoArgs()).Return(5); }) .Verify(delegate { Assert.Equal(5, demo.ReturnIntNoArgs()); }); }
private static void CanCreateAStrictMultiMockFromTwoInterfacesCommon(MockRepository mocks, IDemo demo) { demo.ReturnIntNoArgs(); LastCall.Return(1); IDisposable disposable = demo as IDisposable; Assert.NotNull(disposable); disposable.Dispose(); mocks.ReplayAll(); Assert.Equal(1, demo.ReturnIntNoArgs()); disposable.Dispose(); mocks.VerifyAll(); }
public void CantCallOriginalMethodOnInterface() { MockRepository mocks = new MockRepository(); IDemo demo = (IDemo)mocks.StrictMock(typeof(IDemo)); Assert.Throws <InvalidOperationException>( "Can't use CallOriginalMethod on method ReturnIntNoArgs because the method is abstract.", () => SetupResult.For(demo.ReturnIntNoArgs()).CallOriginalMethod(OriginalCallOptions.CreateExpectation)); }
public void ExpectNever() { demo.Expect(x => x.ReturnStringNoArgs()) .Repeat.Never(); demo.ReturnIntNoArgs(); Assert.Throws <ExpectationViolationException>( () => demo.VerifyExpectations(true)); }
public void UsingTheWithMocksConstruct_GiveCorrectExceptionWhenMockingEvenIfReplayAllNotCalled() { Assert.Throws <IndexOutOfRangeException>("foo", () => With.Mocks(delegate { IDemo demo = Mocker.Current.StrictMock <IDemo>(); Expect.Call(demo.ReturnIntNoArgs()).Return(5); throw new IndexOutOfRangeException("foo"); })); }
public void MockInterface() { MockRepository mocks = new MockRepository(); IDemo demo = (IDemo)mocks.StrictMock(typeof(IDemo)); Expect.Call(demo.ReturnIntNoArgs()).Return(54); demo.VoidStringArg("54"); mocks.ReplayAll(); contextSwitcher.DoStuff(demo); mocks.VerifyAll(); }
public void MockInterfaceExpectException() { MockRepository mocks = new MockRepository(); IDemo demo = (IDemo)mocks.StrictMock(typeof(IDemo)); Expect.Call(demo.ReturnIntNoArgs()).Throw(new InvalidOperationException("That was expected.")); mocks.ReplayAll(); Assert.Throws <InvalidOperationException>( "That was expected.", () => contextSwitcher.DoStuff(demo)); }
public void MockInterfaceUnexpectedCall() { MockRepository mocks = new MockRepository(); IDemo demo = (IDemo)mocks.StrictMock(typeof(IDemo)); Expect.Call(demo.ReturnIntNoArgs()).Return(34); demo.VoidStringArg("bang"); mocks.ReplayAll(); Assert.Throws <ExpectationViolationException>( "IDemo.VoidStringArg(\"34\"); Expected #0, Actual #1.\r\nIDemo.VoidStringArg(\"bang\"); Expected #1, Actual #0.", () => contextSwitcher.DoStuff(demo)); }
public void UsingTheWithMocksConstruct_ThrowsIfExpectationIsMissed() { Assert.Throws <ExpectationViolationException>("IDemo.ReturnIntNoArgs(); Expected #1, Actual #0.", () => With.Mocks(delegate { IDemo demo = Mocker.Current.StrictMock <IDemo>(); Expect.Call(demo.ReturnIntNoArgs()).Return(5); Mocker.Current.ReplayAll(); })); }
private static void CanCreateAStrictMultiMockFromTwoInterfacesCommon(IDemo demo) { demo.Expect(x => x.ReturnIntNoArgs()).Return(1); IDisposable disposable = demo as IDisposable; Assert.NotNull(disposable); disposable.Expect(x => x.Dispose()); Assert.Equal(1, demo.ReturnIntNoArgs()); disposable.Dispose(); demo.VerifyAllExpectations(); }
public void UsingTheWithMocksConstruct_ThrowsIfReplayAllNotCalled() { Assert.Throws <InvalidOperationException>( "This action is invalid when the mock object {Rhino.Mocks.Tests.IDemo} is in record state.", () => { With.Mocks(delegate { IDemo demo = Mocker.Current.StrictMock <IDemo>(); Expect.Call(demo.ReturnIntNoArgs()).Return(5); }); }); }
public void MockInterfaceWithSameName() { MockRepository mocks = new MockRepository(); IDemo demo = (IDemo)mocks.StrictMock(typeof(IDemo)); Expect.Call(demo.ReturnIntNoArgs()).Return(54); demo.VoidStringArg("54"); Other.IDemo remotingDemo = (Other.IDemo)mocks.StrictMock(typeof(Other.IDemo)); remotingDemo.ProcessString("in"); mocks.ReplayAll(); contextSwitcher.DoStuff(demo); contextSwitcher.DoStuff(remotingDemo); mocks.VerifyAll(); }
public void UsingTheWithMocksExceptingVerifyConstruct_ThrowsIfExpectationIsMissed() { MockRepository mocks = new MockRepository(); IDemo demo = mocks.StrictMock <IDemo>(); Assert.Throws <ExpectationViolationException>("IDemo.ReturnIntNoArgs(); Expected #1, Actual #0.", () => With.Mocks(mocks) .Expecting(delegate { Expect.Call(demo.ReturnIntNoArgs()).Return(5); }) .Verify(delegate { })); }
public void UsingTheWithMocksExceptingVerifyConstruct_GiveCorrectExceptionWhenMocking() { MockRepository mocks = new MockRepository(); IDemo demo = mocks.StrictMock <IDemo>(); Assert.Throws <IndexOutOfRangeException>("foo", () => With.Mocks(mocks) .Expecting(delegate { Expect.Call(demo.ReturnIntNoArgs()).Return(5); }) .Verify(delegate { throw new IndexOutOfRangeException("foo"); })); }
private static void CanCreateAStrictMultiMockFromTwoInterfacesCommon(IDemo demo) { demo.Expect(x => x.ReturnIntNoArgs()) .Return(1); IDisposable disposable = demo as IDisposable; Assert.NotNull(disposable); disposable.Expect(x => x.Dispose()); Assert.Equal(1, demo.ReturnIntNoArgs()); disposable.Dispose(); demo.VerifyAllExpectations(); // disposable.VerifyAllExpectations(); }
public void CanCreateADynamicMultiMockFromTwoInterfacesGenericAndAssertWasCalled() { IDemo demo = MockRepository.GenerateMock <IDemo, IEditableObject>(); IEditableObject editable = demo as IEditableObject; demo.ReturnIntNoArgs(); editable.BeginEdit(); editable.CancelEdit(); // we don't care about this editable.EndEdit(); demo.AssertWasCalled(x => x.ReturnIntNoArgs()); editable.AssertWasCalled(x => x.BeginEdit()); editable.AssertWasCalled(x => x.EndEdit()); // Double check all expectations were verified editable.VerifyAllExpectations(); }
public void ExpectNever() { demo.ReturnStringNoArgs(); LastCall.Repeat.Never(); mocks.ReplayAll(); Assert.Throws <ExpectationViolationException>("IDemo.ReturnIntNoArgs(); Expected #0, Actual #1.", () => demo.ReturnIntNoArgs()); }
public void DoStuff(IDemo mock) { int n = mock.ReturnIntNoArgs(); mock.VoidStringArg(n.ToString()); }
private static void CanCreateAStrictMultiMockFromTwoInterfacesCommon(MockRepository mocks, IDemo demo) { demo.ReturnIntNoArgs(); LastCall.Return(1); IDisposable disposable = demo as IDisposable; Assert.NotNull(disposable); disposable.Dispose(); mocks.ReplayAll(); Assert.Equal(1, demo.ReturnIntNoArgs()); disposable.Dispose(); mocks.VerifyAll(); }
public void InvalidReturnValueThrows() { Assert.Throws <InvalidOperationException>( "The delegate return value should be assignable from System.Int32", () => Expect.Call(demo.ReturnIntNoArgs()).Do(new GetDay(GetSunday))); }
public void DoStuff(IDemo mock) { int n = mock.ReturnIntNoArgs(); mock.VoidStringArg(n.ToString()); }
public void CanCallUnexpectedMethodOnDynamicMock() { Assert.Equal(0, demo.ReturnIntNoArgs()); }