예제 #1
0
 public void ThrowsIfNoMatchingConstructorFound()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Debug.WriteLine(new Mock <ClassWithNoDefaultConstructor>(25, true).Object);
     });
 }
예제 #2
0
        public void GetMockCastedToObjectThrows()
        {
            var    mock = new Mock <IAsyncResult>();
            object m    = mock.Object;

            Assert.Throws <ArgumentException>(() => Mock.Get(m));
        }
        public void ShouldUseAditionalArguments2()
        {
            var mock = new Mock <IFoo>(MockBehavior.Strict);

            mock.Setup(x => x.Bar(StartsWith("ma")));
            Assert.Throws <MockException>(() => mock.Object.Bar("no-magic"));
        }
        public void ThrowsIfVerifyVoidExactlyAndLessOrMoreThanNCalls()
        {
            var mock = new Mock <IFoo>();

            mock.Object.Submit();
            mock.Object.Submit();
            mock.Object.Submit();
            mock.Object.Submit();

            var mex = Assert.Throws <MockException>(() => mock.Verify(foo => foo.Submit(), Times.Exactly(5)));

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
            Assert.True(mex.Message.StartsWith(
                            "\nExpected invocation on the mock exactly 5 times, but was 4 times: foo => foo.Submit()"));

            mock.Object.Submit();

            mock.Verify(foo => foo.Submit(), Times.Exactly(5));

            mock.Object.Submit();

            mex = Assert.Throws <MockException>(() => mock.Verify(foo => foo.Submit(), Times.Exactly(5)));
            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
            Assert.True(mex.Message.StartsWith(
                            "\nExpected invocation on the mock exactly 5 times, but was 6 times: foo => foo.Submit()"));
        }
        public void RaisesThrowsIfEventNonVirtual()
        {
            var mock = new Mock <WithEvent>();

            Assert.Throws <ArgumentException>(
                () => mock.SetupSet(m => m.Value = It.IsAny <int>()).Raises(m => m.ClassEvent += null, EventArgs.Empty));
        }
        public void ThrowsIfVerifyVoidBetweenInclusiveAndLessFromOrMoreToCalls()
        {
            var mock = new Mock <IFoo>();

            mock.Object.Submit();

            var mex = Assert.Throws <MockException>(
                () => mock.Verify(foo => foo.Submit(), Times.Between(2, 4, Range.Inclusive)));

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
            Assert.True(mex.Message.StartsWith(
                            "\nExpected invocation on the mock between 2 and 4 times (Inclusive), but was 1 times: foo => foo.Submit()"));

            mock.Object.Submit();

            mock.Verify(foo => foo.Submit(), Times.Between(2, 4, Range.Inclusive));

            mock.Object.Submit();

            mock.Verify(foo => foo.Submit(), Times.Between(2, 4, Range.Inclusive));

            mock.Object.Submit();
            mock.Object.Submit();

            mex = Assert.Throws <MockException>(
                () => mock.Verify(foo => foo.Submit(), Times.Between(2, 4, Range.Inclusive)));
            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
            Assert.True(mex.Message.StartsWith(
                            "\nExpected invocation on the mock between 2 and 4 times (Inclusive), but was 5 times: foo => foo.Submit()"));
        }
        public void ThrowsIfVerifyReturningBetweenInclusiveAndLessFromOrMoreToCalls()
        {
            var mock = new Mock <IFoo>();

            mock.Object.Execute("");

            MockException mex = Assert.Throws <MockException>(() =>
                                                              mock.Verify(foo => foo.Execute(""), Times.Between(2, 4, Range.Inclusive)));

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);

            mock.Object.Execute("");

            mock.Verify(foo => foo.Execute(""), Times.Between(2, 4, Range.Inclusive));

            mock.Object.Execute("");

            mock.Verify(foo => foo.Execute(""), Times.Between(2, 4, Range.Inclusive));

            mock.Object.Execute("");
            mock.Object.Execute("");

            mex = Assert.Throws <MockException>(
                () => mock.Verify(foo => foo.Execute(""), Times.Between(2, 4, Range.Inclusive)));
            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
        }
        public void ThrowsIfVerifyGetGetBetweenInclusiveAndLessFromOrMoreToCalls()
        {
            var mock = new Mock <IFoo>();

            var value = mock.Object.Value;

            MockException mex = Assert.Throws <MockException>(
                () => mock.VerifyGet(foo => foo.Value, Times.Between(2, 4, Range.Inclusive)));

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);

            value = mock.Object.Value;

            mock.VerifyGet(foo => foo.Value, Times.Between(2, 4, Range.Inclusive));

            value = mock.Object.Value;

            mock.VerifyGet(foo => foo.Value, Times.Between(2, 4, Range.Inclusive));

            value = mock.Object.Value;
            value = mock.Object.Value;

            mex = Assert.Throws <MockException>(() =>
                                                mock.VerifyGet(foo => foo.Value, Times.Between(2, 4, Range.Inclusive)));
            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
        }
예제 #9
0
            public void Test()
            {
                var mock = new Mock <IList <string> >();

                Assert.Throws <NotSupportedException>(
                    Resources.SetupOnNonMemberMethod,
                    () => mock.Setup(l => l.FirstOrDefault()).Returns("Hello world"));
            }
        public void ShouldPreserveStackTraceWhenRaisingEvent()
        {
            var mock = new Mock <IAdder <string> >();

            mock.Object.Added += (s, e) => { throw new InvalidOperationException(); };

            Assert.Throws <InvalidOperationException>(() => mock.Raise(m => m.Added += null, EventArgs.Empty));
        }
        public void ThrowsIfVerifyGetTimesNotReached()
        {
            var mock = new Mock <FooBase>();

            mock.Object.GetProtectedValue();

            Assert.Throws <MockException>(() => mock.Protected().VerifyGet <string>("ProtectedValue", Times.Exactly(2)));
        }
        public void ThrowsIfVerifyPropertySetWithExpressionFails()
        {
            var mock = new Mock <IFoo>();

            var e = Assert.Throws <MockException>(() => mock.VerifySet(f => f.Value = It.IsAny <int?>()));

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, e.Reason);
        }
예제 #13
0
        public void FriendlyErrorWhenCallbackArgumentCountNotMatch()
        {
            var mock = new Mock <IFoo>();

            Assert.Throws <ArgumentException>(() =>
                                              mock.Setup(x => x.Submit(It.IsAny <string>()))
                                              .Callback((string s1, string s2) => Debug.WriteLine(s1 + s2)));
        }
예제 #14
0
        public void ThrowsIfExpectationThrowsWithGenericsExceptionType()
        {
            var mock = new Mock <IFoo>();

            mock.Setup(x => x.Submit()).Throws <FormatException>();

            Assert.Throws <FormatException>(() => mock.Object.Submit());
        }
        public void ShouldNotFindPrivateMethodMatcher()
        {
            var foo = new Mock <IFoo>();

            foo.Object.Bar("asd");

            Assert.Throws <MissingMethodException>(() => foo.Verify(f => f.Bar(OddLength())));
        }
        public void ExpectMissingNonStaticMatcherMethod()
        {
            var mock = new Mock <IFoo>(MockBehavior.Strict);

            Assert.Throws <MissingMethodException>(
                "public bool NonStaticMatcherHookWithoutMatcherMethod(System.String) in class Moq.Tests.MatcherAttributeFixture.",
                () => mock.Setup(x => x.Bar(NonStaticMatcherHookWithoutMatcherMethod())));
        }
        public void ExpectMissingMatcherWithArgsMethod()
        {
            var mock = new Mock <IFoo>(MockBehavior.Strict);

            Assert.Throws <MissingMethodException>(
                "public static bool MatcherHook2WithoutMatcherMethod(System.String, System.Int32) in class Moq.Tests.MatcherAttributeFixture.",
                () => mock.Setup(x => x.Bar(MatcherHook2WithoutMatcherMethod(6))));
        }
        public void ThrowsIfVerifyReturningMethodWithExpressionFails()
        {
            var mock = new Mock <IFoo>();

            var mex = Assert.Throws <MockException>(() => mock.Verify(f => f.Execute("ping")));

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
        }
예제 #19
0
        public void ThrowsIfPropertySetterWithWrongValue()
        {
            var mock = new Mock <IFoo>(MockBehavior.Strict);

            mock.SetupSet(m => m.Value = 5);

            Assert.Throws <MockException>(() => { mock.Object.Value = 6; });
        }
        public void ThrowsIfVerifyResultMethodTimesNotReached()
        {
            var mock = new Mock <FooBase>();

            mock.Object.DoProtectedInt();

            Assert.Throws <MockException>(() => mock.Protected().Verify("ProtectedInt", Times.Exactly(2)));
        }
예제 #21
0
            public void TestNoSetups()
            {
                var mock = new Mock <IFoo>();

                var e = Assert.Throws <MockException>(() => mock.Verify(m => m.Execute(1)));

                Assert.Contains("\r\nNo setups configured.", e.Message);
            }
        public void ThrowsIfVerifyVoidMethodWithExpressionFails()
        {
            var mock = new Mock <IFoo>();

            var mex = Assert.Throws <MockException>(() => mock.Verify(f => f.Submit()));

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
        }
예제 #23
0
        public void ShouldThrowIfAsIsInvokedAfterInstanceIsRetrieved()
        {
            var mock = new Mock <IBag>();

            var instance = mock.Object;

            Assert.Throws <InvalidOperationException>(() => mock.As <IFoo>());
        }
        public void IncludesMessageAboutNoActualCallsInFailureMessage()
        {
            var mock = new Moq.Mock <IFoo>();

            MockException mex = Assert.Throws <MockException>(() => mock.Verify(f => f.Execute("pong")));

            Assert.Contains(Environment.NewLine + "No invocations performed.", mex.Message);
        }
        public void VerifiesHierarchyMethodWithExpression()
        {
            var mock = new Mock <IFoo>();

            Assert.Throws <MockException>(() => mock.Verify(m => m.Bar.Do("ping")));

            mock.Object.Bar.Do("ping");
            mock.Verify(m => m.Bar.Do("ping"));
        }
        public void VerifiesHierarchyPropertySetWithExpression()
        {
            var mock = new Mock <IFoo>();

            Assert.Throws <MockException>(() => mock.VerifySet(m => m.Bar.Value = It.IsAny <int>()));

            mock.Object.Bar.Value           = 5;
            mock.VerifySet(m => m.Bar.Value = It.IsAny <int>());
        }
        public void VerifiesPropertySetWithExpressionAndMessage()
        {
            var mock = new Mock <IFoo>();

            var me = Assert.Throws <MockException>(() => mock.VerifySet(f => f.Value = It.IsAny <int?>(), "Nobody called .Value"));

            Assert.True(me.Message.Contains("Nobody called .Value"));
            Assert.True(me.Message.Contains("f.Value"));
        }
        public void VerifiesPropertySetValueWithExpressionAndMessage()
        {
            var mock = new Mock <IFoo>();

            var e = Assert.Throws <MockException>(() => mock.VerifySet(f => f.Value = 5, "Nobody called .Value"));

            Assert.Contains("Nobody called .Value", e.Message);
            Assert.Contains("f.Value", e.Message);
        }
        public void VerifiesSetterWithAction()
        {
            var mock = new Mock <IFoo>();

            Assert.Throws <MockException>(() => mock.VerifySet(m => m.Value = 2));
            mock.Object.Value = 2;

            mock.VerifySet(m => m.Value = 2);
        }
        public void ThrowsIfVerifyAllNotMet()
        {
            var mock = new Mock <IFoo>();

            mock.Setup(x => x.Submit());

            var mex = Assert.Throws <MockVerificationException>(() => mock.VerifyAll());

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
        }