Ejemplo n.º 1
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 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 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 ThrowsIfVerifiableExpectationNotCalledWithMessage()
        {
            var mock = new Mock <IFoo>();

            mock.Setup(x => x.Submit()).Verifiable("Kaboom!");

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

            Assert.Contains("Kaboom!", mex.Message);
            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
        }
        public void VerifiesSetterWithActionAndMessage()
        {
            var mock = new Mock <IFoo>();

            var me = Assert.Throws <MockException>(() => mock.VerifySet(m => m.Value = 2, "foo"));

            Assert.Contains("foo", me.Message);

            mock.Object.Value = 2;

            mock.VerifySet(m => m.Value = 2, "foo");
        }
Ejemplo n.º 6
0
            public void Test()
            {
                var target = new Mock <IFoo>();

                target.Setup(t => t.Submit(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()));

                var e = Assert.Throws <MockVerificationException>(() => target.VerifyAll());

                Assert.Contains(
                    "IFoo t => t.Submit(It.IsAny<String>(), It.IsAny<String>(), new[] { It.IsAny<Int32>() })",
                    e.Message);
            }
        public void ThrowsWithExpressionIfVerifiableExpectationWithLambdaMatcherNotCalled()
        {
            var mock = new Mock <IFoo>();

            mock.Setup(x => x.Execute(It.Is <string>(s => string.IsNullOrEmpty(s))))
            .Returns("ack")
            .Verifiable();

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

            Assert.Equal(MockException.ExceptionReason.VerificationFailed, mex.Reason);
            Assert.Contains(@".Execute(It.Is<String>(s => String.IsNullOrEmpty(s)))", mex.Message);
        }
Ejemplo n.º 8
0
            public void TestGeneric()
            {
                var mock = new Mock <IFoo>();

                mock.Setup(m => m.Execute <int>(1, 10));
                mock.Setup(m => m.Execute <string>(1, "Foo"));

                mock.Object.Execute(1, 10);

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

                Assert.Contains(
                    "\r\nConfigured setups:\r\nm => m.Execute<Int32>(1, 10), Times.Once",
                    e.Message);
            }
        public void IncludesActualCallsInFailureMessage()
        {
            var mock = new Moq.Mock <IFoo>();

            mock.Object.Execute("ping");
            mock.Object.Echo(42);
            mock.Object.Submit();

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

            Assert.Contains(
                Environment.NewLine +
                "Performed invocations:" + Environment.NewLine +
                "IFoo.Execute(\"ping\")" + Environment.NewLine +
                "IFoo.Echo(42)" + Environment.NewLine +
                "IFoo.Submit()",
                mex.Message);
        }
Ejemplo n.º 10
0
        public void ThrowsWithTargetTypeName()
        {
            var bag = new Mock <IBag>();
            var foo = bag.As <IFoo>();

            bag.Setup(b => b.Add("foo", "bar")).Verifiable();
            foo.Setup(f => f.Execute()).Verifiable();

            try
            {
                bag.Verify();
            }
            catch (MockVerificationException me)
            {
                Assert.Contains(typeof(IFoo).Name, me.Message);
                Assert.Contains(typeof(IBag).Name, me.Message);
            }
        }
Ejemplo n.º 11
0
            public void Test()
            {
                var mock = new Mock <IFoo>();

                mock.Setup(m => m.Execute(1));
                mock.Setup(m => m.Execute(It.IsInRange(2, 20, Range.Exclusive)));
                mock.Setup(m => m.Execute(3, "Caption"));

                mock.Object.Execute(3);
                mock.Object.Execute(4);
                mock.Object.Execute(5);

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

                Assert.Contains(
                    "\r\nConfigured setups:" +
                    "\r\nm => m.Execute(1), Times.Never" +
                    "\r\nm => m.Execute(It.IsInRange<Int32>(2, 20, Range.Exclusive)), Times.Exactly(3)",
                    e.Message);
            }