예제 #1
0
        public void ClassWithCtorMockedShouldImplementMocked()
        {
            Mock <FooWithCtor> mock   = new Mock <FooWithCtor>(5);
            FooWithCtor        mocked = mock.Object;

            Assert.True(mocked is IMocked <FooWithCtor>);
        }
예제 #2
0
        public void ClassMockedShouldImplementMocked()
        {
            Mock <Foo> mock   = new Mock <Foo>();
            Foo        mocked = mock.Object;

            Assert.True(mocked is IMocked <Foo>);
        }
        public void ShouldRaiseEventWithFuncFourArgs()
        {
            var mock = new Mock <IAdder <string> >();

            mock.Setup(add => add.Do(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <bool>(), It.IsAny <string>()))
            .Raises(m => m.Added += null, (string s, int i, bool b, string v) => new FooArgs {
                Args = new object[] { s, i, b, v }
            });

            var raised = false;

            mock.Object.Added += (sender, args) =>
            {
                raised = true;
                Assert.True(args is FooArgs);
                Assert.Equal("foo", ((FooArgs)args).Args[0]);
                Assert.Equal(5, ((FooArgs)args).Args[1]);
                Assert.Equal(true, ((FooArgs)args).Args[2]);
                Assert.Equal("bar", ((FooArgs)args).Args[3]);
            };

            mock.Object.Do("foo", 5, true, "bar");

            Assert.True(raised);
        }
        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()"));
        }
예제 #5
0
        public void InterfaceMockedShouldImplementMocked()
        {
            Mock <IFoo> mock   = new Mock <IFoo>();
            IFoo        mocked = mock.Object;

            Assert.True(mocked is IMocked <IFoo>);
        }
        public void ShouldSupportMultipleMethod()
        {
            var target = Mocks.Of <IBaz>().First(x => !x.HasElements("1") && x.HasElements("2"));

            Assert.False(target.HasElements("1"));
            Assert.True(target.HasElements("2"));
        }
        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()"));
        }
예제 #8
0
            public void WhenCombiningQueryingWithExplicitBoolean_ThenSetsBothProperties()
            {
                var target = Mock.Of <IFoo>(x => x.IsValid == true && x.Value == "foo");

                Assert.True(target.IsValid);
                Assert.Equal("foo", target.Value);
            }
        public void ChooseAffirmativeExpetationOnPropertySetter()
        {
            var mock = new Mock <IFoo>();

            var when     = true;
            var positive = false;
            var negative = false;

            mock.When(() => when).SetupSet(x => x.Value  = "foo").Callback(() => positive = true);
            mock.When(() => !when).SetupSet(x => x.Value = "foo").Callback(() => negative = true);

            mock.Object.Value = "foo";

            Assert.True(positive);
            Assert.False(negative);

            when              = false;
            positive          = false;
            mock.Object.Value = "foo";

            Assert.False(positive);
            Assert.True(negative);

            when              = true;
            negative          = false;
            mock.Object.Value = "foo";

            Assert.True(positive);
            Assert.False(negative);
        }
        public void ChooseAffirmativeExpetationOnVoidMethod()
        {
            var mock = new Mock <IFoo>();

            var when     = true;
            var positive = false;
            var negative = false;

            mock.When(() => when).Setup(x => x.Bar()).Callback(() => positive  = true);
            mock.When(() => !when).Setup(x => x.Bar()).Callback(() => negative = true);

            mock.Object.Bar();

            Assert.True(positive);
            Assert.False(negative);

            when     = false;
            positive = false;
            mock.Object.Bar();

            Assert.False(positive);
            Assert.True(negative);

            when     = true;
            negative = false;
            mock.Object.Bar();

            Assert.True(positive);
            Assert.False(negative);
        }
예제 #11
0
        public void ProvidesEmptyArray()
        {
            var provider = new EmptyDefaultValueProvider();

            var value = provider.ProvideDefault(typeof(IFoo).GetProperty("Bars").GetGetMethod());

            Assert.True(value is IBar[] && ((IBar[])value).Length == 0);
        }
예제 #12
0
        public void ProvidesEmptyEnumerable()
        {
            var provider = new EmptyDefaultValueProvider();

            var value = provider.ProvideDefault(typeof(IFoo).GetProperty("Indexes").GetGetMethod());

            Assert.True(value is IEnumerable <int> && ((IEnumerable <int>)value).Count() == 0);
        }
예제 #13
0
        public void UsesCustomMatcher()
        {
            var mock = new Mock <IFoo>();

            mock.Setup(m => m.Do(Any <string>())).Returns(true);

            Assert.True(mock.Object.Do("foo"));
        }
예제 #14
0
        public void MockObjectsEqualityIsReferenceEquals()
        {
            var mock1 = new Mock <IFoo>();
            var mock2 = new Mock <IFoo>();

            Assert.True(mock1.Object.Equals(mock1.Object));
            Assert.False(mock1.Object.Equals(mock2.Object));
        }
예제 #15
0
        public void ReturnsServiceFromServiceProvider()
        {
            var provider = new Mock <IServiceProvider>();

            provider.Setup(x => x.GetService(typeof(IFooService))).Returns(new FooService());

            Assert.True(provider.Object.GetService(typeof(IFooService)) is FooService);
        }
        public void OnceChecksOneTime()
        {
            var target = Times.Once();

            Assert.False(target.Verify(-1));
            Assert.False(target.Verify(0));
            Assert.True(target.Verify(1));
            Assert.False(target.Verify(int.MaxValue));
        }
        public void NeverChecksZeroTimes()
        {
            var target = Times.Never();

            Assert.False(target.Verify(-1));
            Assert.True(target.Verify(0));
            Assert.False(target.Verify(1));
            Assert.False(target.Verify(int.MaxValue));
        }
        public void ShouldTranslateToUseMatcherImplementation()
        {
            var mock = new Mock <IFoo>(MockBehavior.Strict);

            mock.Setup(x => x.Bar(IsMagicString()));
            IsMagicStringCalled = false;
            mock.Object.Bar("magic");
            Assert.True(IsMagicStringCalled);
        }
        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 AtLeastRangesBetweenTimesAndMaxValue()
        {
            var target = Times.AtLeast(10);

            Assert.False(target.Verify(-1));
            Assert.False(target.Verify(0));
            Assert.False(target.Verify(9));
            Assert.True(target.Verify(10));
            Assert.True(target.Verify(int.MaxValue));
        }
예제 #21
0
            public void TestVerifyMessage()
            {
                var mock = new Mock <Foo>();

                mock.Setup(m => m.OnExecute());

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

                Assert.True(e.Message.StartsWith("Invalid verify"));
            }
예제 #22
0
            public void ShouldMockHtmlControl()
            {
                // CallBase was missing
                var htmlInputTextMock = new Mock <HtmlInputText>()
                {
                    CallBase = true
                };

                Assert.True(htmlInputTextMock.Object.Visible);
            }
        public void AtMostOnceRangesBetweenZeroAndOne()
        {
            var target = Times.AtMostOnce();

            Assert.False(target.Verify(-1));
            Assert.True(target.Verify(0));
            Assert.True(target.Verify(1));
            Assert.False(target.Verify(5));
            Assert.False(target.Verify(int.MaxValue));
        }
        public void AsInterfaceVerifiesPropertySetValueWithExpressionAndMessage()
        {
            var disposable = new Mock <IDisposable>();
            var mock       = disposable.As <IBar>();

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

            Assert.True(e.Message.Contains("Nobody called .Value"));
            Assert.True(e.Message.Contains("f.Value"));
        }
        public void AsInferfaceVerifiesVoidMethodWithExpressionAndMessage()
        {
            var disposable = new Mock <IDisposable>();
            var mock       = disposable.As <IFoo>();

            var e = Assert.Throws <MockException>(() => mock.Verify(f => f.Submit(), "Submit should be invoked"));

            Assert.True(e.Message.Contains("Submit should be invoked"));
            Assert.True(e.Message.Contains("f.Submit()"));
        }
        public void VerifiesVoidMethodWithExpressionAndMessage()
        {
            var mock = new Mock <IFoo>();

            var me = Assert.Throws <MockException>(
                () => mock.Verify(f => f.Submit(), "Submit should be invoked"));

            Assert.True(me.Message.Contains("Submit should be invoked"));
            Assert.True(me.Message.Contains("f.Submit()"));
        }
        public void VerifiesReturningMethodWithExpressionAndMessage()
        {
            var mock = new Mock <IFoo>();

            var me = Assert.Throws <MockException>(
                () => mock.Verify(f => f.Execute("ping"), "Execute should have been invoked with 'ping'"));

            Assert.True(me.Message.Contains("Execute should have been invoked with 'ping'"));
            Assert.True(me.Message.Contains("f.Execute(\"ping\")"));
        }
예제 #28
0
        public void ExecutesCallbackWhenVoidMethodIsCalled()
        {
            var  mock   = new Mock <IFoo>();
            bool called = false;

            mock.Setup(x => x.Submit()).Callback(() => called = true);

            mock.Object.Submit();
            Assert.True(called);
        }
예제 #29
0
        public void CallbackCalledWithoutArgumentsForMethodCallWithArguments()
        {
            var  mock   = new Mock <IFoo>();
            bool called = false;

            mock.Setup(x => x.Submit(It.IsAny <string>())).Callback(() => called = true);

            mock.Object.Submit("blah");
            Assert.True(called);
        }
예제 #30
0
        public void ExecutesCallbackWhenNonVoidMethodIsCalled()
        {
            var  mock   = new Mock <IFoo>();
            bool called = false;

            mock.Setup(x => x.Execute("ping")).Callback(() => called = true).Returns("ack");

            Assert.Equal("ack", mock.Object.Execute("ping"));
            Assert.True(called);
        }