コード例 #1
0
        public void WhenNoExpectationIsSetup_WillReturnDefaultValues()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            Assert.Equal(0, demo.DoSomething());
        }
コード例 #2
0
        public void WillFailVerificationsOfMethod_IfWereNotCalled_OnVoidMethod()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.ReplayAll();
            Assert.Throws <ExpectationViolationException>(
                "IFoo54.DoSomethingElse(); Expected #1, Actual #0.",
                () => demo.AssertWasCalled(x => x.DoSomethingElse()));
        }
コード例 #3
0
        public void CanUseNonRecordReplayModel_Expect_OnVoidMethod()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Expect(x => x.DoSomethingElse());
            mocks.Replay(demo);
            demo.DoSomethingElse();
            mocks.VerifyAll();
        }
コード例 #4
0
        public void CanUseNonRecordReplayModel_Stub_WillNotThrowIfExpectationIsNotMet()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Stub(x => x.DoSomething()).Return(1);
            mocks.Replay(demo);

            mocks.VerifyAll();
        }
コード例 #5
0
        public void CanUseNonRecordReplayModel_Stub()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Stub(x => x.DoSomething()).Return(1);

            mocks.Replay(demo);
            Assert.Equal(1, demo.DoSomething());
        }
コード例 #6
0
        public void CannotUseRepeatAny()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.ReplayAll();
            Assert.Throws <InvalidOperationException>(
                "The expectation was removed from the waiting expectations list, did you call Repeat.Any() ? This is not supported in AssertWasCalled()",
                () => demo.AssertWasCalled(x => x.Bar("a"), o => o.Repeat.Any()));
        }
コード例 #7
0
        public void CanAssertNotCalledOnPropertyAccess()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.Replay(demo);

            demo.DoSomething();

            demo.AssertWasNotCalled(x => x.FooBar);
        }
コード例 #8
0
        public void CanAssertOnPropertyAccess()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.Replay(demo);

            var result = demo.FooBar;

            demo.AssertWasCalled(x => x.FooBar);
        }
コード例 #9
0
        public void CanAssertOnMethodCall()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.Replay(demo);

            demo.DoSomething();

            demo.AssertWasCalled(x => x.DoSomething());
        }
コード例 #10
0
        public void WillRaiseErrorIfNoExpectationWasSetup()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.Replay(demo);

            Assert.Throws <InvalidOperationException>(
                "No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call",
                () => demo.AssertWasCalled(x => { }));
        }
コード例 #11
0
        public void UsingExpectWithoutSettingReturnValueThrows()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Expect(x => x.DoSomething());
            mocks.Replay(demo);
            Assert.Throws <InvalidOperationException>(
                "Method 'IFoo54.DoSomething();' requires a return value or an exception to throw.",
                () => Assert.Equal(1, demo.DoSomething()));
        }
コード例 #12
0
        public void CanAssertOnMethodCallUsingConstraints()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.Replay(demo);

            demo.Bar("blah baba");

            demo.AssertWasCalled(x => x.Bar(Arg <string> .Matches((string a) => a.StartsWith("b") && a.Contains("ba"))));
        }
コード例 #13
0
        public void CanUseNonRecordReplayModel_Stub_AndThenVerify_WhenNotCalled_WillCauseError()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Stub(x => x.DoSomething()).Return(1);
            mocks.Replay(demo);

            Assert.Throws <ExpectationViolationException>(
                "IFoo54.DoSomething(); Expected #1, Actual #0.",
                () => demo.AssertWasCalled(x => x.DoSomething()));
        }
コード例 #14
0
        public void CanUseNonRecordReplayModel_Expect_OnVoidMethod_WhenMethodNotcall_WillFailTest()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Expect(x => x.DoSomethingElse());
            mocks.Replay(demo);

            Assert.Throws <ExpectationViolationException>(
                "IFoo54.DoSomethingElse(); Expected #1, Actual #0.",
                () => mocks.VerifyAll());
        }
コード例 #15
0
        public void TypeShouldBeInferredFromMockNotReference()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <Foo54>(0);

            demo.Stub(x => x.DoSomethingElse());
            mocks.Replay(demo);

            demo.DoSomethingElse();

            demo.AssertWasCalled(x => x.DoSomethingElse());
        }
コード例 #16
0
        public void CanUseNonRecordReplayModel_Expect()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Expect(x => x.DoSomething()).Return(1).Repeat.Once();
            mocks.Replay(demo);
            Assert.Equal(1, demo.DoSomething());
            demo.Expect(x => x.DoSomething()).Return(15).Repeat.Once();
            Assert.Equal(15, demo.DoSomething());

            mocks.VerifyAll();
        }
コード例 #17
0
        public void CanAssertOnPropertyAccessWithConstraints()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.Replay(demo);

            var result = demo.FooBar;

            result = demo.FooBar;

            demo.AssertWasCalled(x => x.FooBar, o => o.Repeat.Twice());
        }
コード例 #18
0
        public void CanAssertOnMethodCallUsingConstraints_WhenMethodNotFound()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.ReplayAll();

            demo.Bar("yoho");

            Assert.Throws <ExpectationViolationException>(
                "IFoo54.Bar(a => (a.StartsWith(\"b\") && a.Contains(\"ba\"))); Expected #1, Actual #0."
                , () => demo.AssertWasCalled(x => x.Bar(Arg <string> .Matches((string a) => a.StartsWith("b") && a.Contains("ba")))));
        }
コード例 #19
0
        public void CanOnlyPassSingleExpectationToVerify()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            mocks.Replay(demo);

            Assert.Throws <InvalidOperationException>(
                "You can only use a single expectation on AssertWasCalled(), use separate calls to AssertWasCalled() if you want to verify several expectations",
                () => demo.AssertWasCalled(x =>
            {
                x.DoSomethingElse();
                x.DoSomethingElse();
            }));
        }
コード例 #20
0
        public void CanUseNonRecordReplayModel_Stub_OnVoidMethod()
        {
            MockRepository mocks = new MockRepository();
            IFoo54         demo  = mocks.DynamicMock <IFoo54>();

            demo.Stub(x => x.DoSomethingElse()).Throw(new InvalidOperationException("foo"));
            mocks.Replay(demo);

            try
            {
                demo.DoSomethingElse();
                Assert.False(true, "Should throw");
            }
            catch (InvalidOperationException e)
            {
                Assert.Equal("foo", e.Message);
            }
        }