예제 #1
0
        private void SetToMatcherTest(Mock <IParentInterface> mock)
        {
            Version version = new Version(1, 1, 1, 1);

            mock.Expects.One.SetProperty(_ => _.ReadWriteObjectProperty).To(Is.EqualTo(version));
            mock.MockObject.ReadWriteObjectProperty = version;
        }
예제 #2
0
        public void MatchesObjectWithMatchingField()
        {
            ObjectWithFields o = new ObjectWithFields();

            o.PublicField = "actual value";
            Matcher m = new FieldMatcher("PublicField", Is.EqualTo("actual value"));

            Assert.IsTrue(m.Matches(o), "should match o");
        }
예제 #3
0
        public void ReturnsCloneOfPrototypeObject()
        {
            ICloneable prototype = ACloneableObject();
            IAction    action    = new ReturnCloneAction(prototype);

            object result = ReturnActionTest.ResultOfAction(action);

            Verify.That(result, !Is.EqualTo(prototype));
        }
예제 #4
0
        public void DoesNotMatchIfValueMatchersDoNotMatchArgumentValues()
        {
            Matcher matcher = new ArgumentsMatcher(Is.EqualTo(arg1Value), Is.EqualTo(arg2Value));

            Assert.IsFalse(matcher.Matches(InvocationWithArguments("other object", arg2Value)),
                           "different first arg");
            Assert.IsFalse(matcher.Matches(InvocationWithArguments(arg1Value, "other object")),
                           "different second arg");
        }
예제 #5
0
        public void MatchesObjectWithNamedPropertyAndMatchingPropertyValue()
        {
            ObjectWithProperties o = new ObjectWithProperties();
            object aValue          = new NamedObject("aValue");

            o.A = aValue;

            Matcher m = new PropertyMatcher("A", Is.EqualTo(aValue));

            Assert.IsTrue(m.Matches(o), "should match o");
        }
예제 #6
0
        public void touchedWhenOneSpriteTouched()
        {
            var collection = new SpriteCollection(sprites, camera.MockObject, sensor.MockObject);

            sensor.Expects.AtLeastOne.Method(_ => _.insideSprite(null, null, null))
            .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.Anything).WillReturn(true);
            sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
            .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.Anything).WillReturn(false);

            Assert.That(collection.touchedAtSameTime(0f), Iz.False);
        }
예제 #7
0
        public void DoesNotMatchAndDoesNotThrowExceptionIfValueSpecifiedForOutputParameter()
        {
            Matcher matcher = new ArgumentsMatcher(Is.EqualTo(arg1Value), Is.EqualTo(arg2Value));

            Invocation invocation = new Invocation(
                new NamedObject("receiver"),
                new MethodInfoStub("method",
                                   new ParameterInfoStub("in", ParameterAttributes.In),
                                   new ParameterInfoStub("out", ParameterAttributes.Out)),
                new[] { arg1Value, null });

            Assert.IsFalse(matcher.Matches(invocation));
        }
예제 #8
0
        public void MatchesOutputParametersWithSpecialMatcherClass()
        {
            Matcher matcher = new ArgumentsMatcher(Is.EqualTo(arg1Value), Is.Out);

            Invocation invocation = new Invocation(
                new NamedObject("receiver"),
                new MethodInfoStub("method",
                                   new ParameterInfoStub("in", ParameterAttributes.In),
                                   new ParameterInfoStub("out", ParameterAttributes.Out)),
                new[] { arg1Value, null });

            Assert.IsTrue(matcher.Matches(invocation));
        }
예제 #9
0
        public void TestRenderNormal()
        {
            // Make sure the renderer draws at least the label's text
            MockedGraphics.Expects.One.Method(
                m => m.DrawString(null, RectangleF.Empty, null)
                ).With(Is.Anything, Is.Anything, Is.EqualTo("Test"));

            // Let the renderer draw the label into the mocked graphics interface
            renderer.Render(this.label, MockedGraphics.MockObject);

            // And verify the expected drawing commands were issues
            Mockery.VerifyAllExpectationsHaveBeenMet();
        }
예제 #10
0
        public void StubsMatchArgumentsAndPerformActionsJustLikeAnExpectation()
        {
            Mock <IHelloWorld> helloWorld = Mocks.CreateMock <IHelloWorld>();

            helloWorld.Stub.Out.Method(_ => _.Ask(null)).With("Name").Will(Return.Value("Bob"));
            helloWorld.Stub.Out.Method(_ => _.Ask(null)).With("Age").Will(Return.Value("30"));

            for (int i = 0; i < ANY_NUMBER; i++)
            {
                Verify.That(helloWorld.MockObject.Ask("Name"), Is.EqualTo("Bob"), "Name");
                Verify.That(helloWorld.MockObject.Ask("Age"), Is.EqualTo("30"), "Age");
            }
        }
예제 #11
0
        public void ShouldGetArgumentsOfInvokedMethod()
        {
            MockFactory            mocks       = new MockFactory();
            string                 message     = "hello";
            Mock <IMessageService> serviceMock = mocks.CreateMock <IMessageService>();

            MessageController controller = new MessageController(serviceMock.MockObject);

            serviceMock.Expects.One.Method(_ => _.GetMessage(0, null)).With(Is.EqualTo(23), Is.Anything)
            .Will(GetArguments.WhenCalled(arguments => (arguments[1] as Action <string>).Invoke(message)));

            controller.GetMessage(23);

            Assert.AreEqual(message, controller.Message);
        }
        /// <summary>
        ///   Lets the renderer draw the button and verifies that the button used
        ///   the skin elements from the expected state
        /// </summary>
        /// <param name="expectedState">
        ///   Expected state the skin elements should be using
        /// </param>
        private void drawAndExpectState(string expectedState)
        {
            // Make sure the renderer draws at least a frame and the button's text
            MockedGraphics.Expects.One.Method(m => m.DrawElement(null, RectangleF.Empty)).With(
                Is.StringContaining(expectedState), Is.Anything
                );
            MockedGraphics.Expects.One.Method(m => m.DrawString(null, RectangleF.Empty, null)).With(
                Is.StringContaining(expectedState), Is.Anything, Is.EqualTo("Test")
                );

            // Let the renderer draw the button into the mocked graphics interface
            renderer.Render(this.button, MockedGraphics.MockObject);

            // And verify the expected drawing commands were issues
            Mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void TestRenderNormal()
        {
            // Make sure the renderer draws at least a frame and the window's title
            MockedGraphics.Expects.One.Method(
                m => m.DrawElement(null, RectangleF.Empty)
                ).WithAnyArguments();

            MockedGraphics.Expects.One.Method(
                m => m.DrawString(null, RectangleF.Empty, null)
                ).With(Is.Anything, Is.Anything, Is.EqualTo("Title"));

            // Let the renderer draw the window into the mocked graphics interface
            renderer.Render(this.window, MockedGraphics.MockObject);

            // And verify the expected drawing commands were issues
            Mockery.VerifyAllExpectationsHaveBeenMet();
        }
예제 #14
0
        private void WithArgumentsTest(IParentInterface instance)
        {
            using (Factory.Ordered())
            {
                Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With(1, 2, 3);
                Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With(Is.EqualTo(4), Is.AtLeast(5), Is.AtMost(6));
                Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With(7, Is.AtLeast(8), 9);
                Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With(
                    new PredicateMatcher <int>(parameter => parameter == 10),
                    Is.Match <int>(parameter => parameter == 11),
                    Is.Anything);
            }

            instance.MethodVoid(1, 2, 3);
            instance.MethodVoid(4, 5, 6);
            instance.MethodVoid(7, 8, 9);
            instance.MethodVoid(10, 11, 12);
        }
예제 #15
0
        public void VerifyThatFailsIfMatcherDoesNotMatchValue()
        {
            object  expected = new NamedObject("expected");
            object  actual   = new NamedObject("actual");
            Matcher matcher  = Is.EqualTo(expected);

            try
            {
                Verify.That(actual, matcher);
            }
            catch (ExpectationException e)
            {
                Assert.AreEqual(
                    Environment.NewLine +
                    "Expected: " + matcher + Environment.NewLine +
                    "Actual:   <" + actual + ">(NMockTests.NamedObject)",
                    e.Message);
                return;
            }

            Assert.Fail("Verify.That should have failed");
        }
예제 #16
0
        public void CanPrependCustomMessageToDescriptionOfFailure()
        {
            object  expected = new NamedObject("expected");
            object  actual   = new NamedObject("actual");
            Matcher matcher  = Is.EqualTo(expected);

            try
            {
                Verify.That(actual, matcher, "message {0} {1}", "0", 1);
            }
            catch (ExpectationException e)
            {
                Assert.AreEqual(
                    "message 0 1" + Environment.NewLine +
                    "Expected: " + matcher + Environment.NewLine +
                    "Actual:   <" + actual + ">(NMockTests.NamedObject)",
                    e.Message);
                return;
            }

            Assert.Fail("Verify.That should have failed");
        }
예제 #17
0
        private void WithArgumentsTest2(Mock <IParentInterface> mock)
        {
            var expectations = new Expectations(4);

            using (Factory.Ordered())
            {
                expectations[0] = mock.Arrange(_ => _.MethodVoid(0, 0, 0)).With(1, 2, 3);
                expectations[1] = mock.Arrange(_ => _.MethodVoid(0, 0, 0)).With(Is.EqualTo(4), Is.AtLeast(5), Is.AtMost(6));
                expectations[2] = mock.Arrange(_ => _.MethodVoid(0, 0, 0)).With(7, Is.AtLeast(8), 9);
                expectations[3] = mock.Arrange(_ => _.MethodVoid(0, 0, 0)).With(
                    new PredicateMatcher <int>(parameter => parameter == 10),
                    Is.Match <int>(parameter => parameter == 11),
                    Is.Anything);
            }

            mock.MockObject.MethodVoid(1, 2, 3);
            mock.MockObject.MethodVoid(4, 5, 6);
            mock.MockObject.MethodVoid(7, 8, 9);
            mock.MockObject.MethodVoid(10, 11, 12);

            expectations.ForEach(_ => _.Assert());
        }
예제 #18
0
        public void notTouchedWhenOneThenBothTooLate()
        {
            var collection = new SpriteCollection(sprites, null, sensor.MockObject);

            using (factory.Ordered()) {
                var heldDown     = new[] { TouchPhase.Began, TouchPhase.Moved, TouchPhase.Stationary };
                var initialTouch = new[] { TouchPhase.Began };

                // first finger down
                sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
                .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(heldDown))
                .WillReturn(true);
                sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
                .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(initialTouch))
                .WillReturn(true);
                sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
                .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.EqualTo(heldDown))
                .WillReturn(false);

                // second finger down
                sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
                .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(heldDown))
                .WillReturn(true);
                sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
                .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(initialTouch))
                .WillReturn(false);
                sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
                .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.EqualTo(heldDown))
                .WillReturn(true);
                sensor.Expects.One.Method(_ => _.insideSprite(null, null, null))
                .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.EqualTo(initialTouch))
                .WillReturn(true);
            }

            collection.touchedAtSameTime(0f);
            Assert.That(collection.touchedAtSameTime(0.3f), Iz.False);
        }
예제 #19
0
        private void OrderedTest1(Mock <IParentInterface> mock)
        {
            mock.Expects.One.DelegateBinding(_ => _.Delegate += null);
            mock.Expects.One.MethodWith(_ => _.Echo("0")).WillReturn("0");

            using (Factory.Ordered())
            {
                mock.Expects.One.MethodWith(_ => _.Echo("1")).WillReturn("1");
                mock.Expects.One.Method(_ => _.Echo(null)).With(Is.EqualTo("2")).WillReturn("2");
                mock.Expects.One.Method(_ => _.Echo(null)).With("3").WillReturn("3");

                using (Factory.Unordered())
                {
                    mock.Expects.One.MethodWith(_ => _.Echo("4")).WillReturn("4");
                    mock.Expects.One.Method(_ => _.Echo(null)).With(Is.EqualTo("5")).WillReturn("5");
                    mock.Expects.One.Method(_ => _.Echo(null)).With("6").WillReturn("6");
                }

                using (Factory.Ordered())
                {
                    //do nothing
                }

                mock.Expects.One.MethodWith(_ => _.Echo("7")).WillReturn("7");
                mock.Expects.One.MethodWith(_ => _.Echo("8")).WillReturn("8");

                using (Factory.Unordered())
                {
                    mock.Expects.One.MethodWith(_ => _.Echo("9")).WillReturn("9");
                    mock.Expects.One.MethodWith(_ => _.Echo("10")).WillReturn("10");

                    using (Factory.Ordered())
                    {
                        // do nothing
                    }
                    mock.Expects.One.MethodWith(_ => _.Echo("11")).WillReturn("11");
                    mock.Expects.One.MethodWith(_ => _.Echo("12")).WillReturn("12");
                }

                using (Factory.Unordered())
                {
                    //do nothing
                }
                mock.Expects.One.MethodWith(_ => _.Echo("13")).WillReturn("13");
            }

            mock.MockObject.Delegate += (s) => { };
            mock.MockObject.Echo("0");
            mock.MockObject.Echo("1");
            mock.MockObject.Echo("2");
            mock.MockObject.Echo("3");
            mock.MockObject.Echo("6");
            mock.MockObject.Echo("4");
            mock.MockObject.Echo("5");
            mock.MockObject.Echo("7");
            mock.MockObject.Echo("8");
            mock.MockObject.Echo("12");
            mock.MockObject.Echo("11");
            mock.MockObject.Echo("10");
            mock.MockObject.Echo("9");
            mock.MockObject.Echo("13");
        }
예제 #20
0
        public void VerifyThatPassesIfMatcherMatchesValue()
        {
            object value = new NamedObject("value");

            Verify.That(value, Is.EqualTo(value));
        }
예제 #21
0
 private void AssertReturnsValue(IAction action, Type returnType, object expectedResult)
 {
     AssertReturnsValue(action, returnType, Is.EqualTo(expectedResult));
 }
예제 #22
0
        public void triggersFrontMostObjectWhenObjectsOverlap()
        {
            var messagePromptCoordinator = new MessagePromptCoordinator(prompt.MockObject, messageBox.MockObject);

            var first = factory.CreateMock <GameObject>();

            first.Expects.Any.Method(_ => _.GetComponent <Sprite>()).WillReturn(sprite.MockObject);
            var second = factory.CreateMock <GameObject>();

            second.Expects.Any.Method(_ => _.GetComponent <Sprite>()).WillReturn(sprite.MockObject);

            var back          = new Vector3(0, 0, 0);
            var backTransform = new Transform()
            {
                position = back
            };
            var front          = new Vector3(0, 0, -1);
            var frontTransform = new Transform()
            {
                position = front
            };

            // if you think this looks weird, it is. we can't mock bareback public fields, and Unity is full of them.
            first.MockObject.transform  = backTransform;
            second.MockObject.transform = frontTransform;

            var frontObjectResponse = "front object response";
            var frontObjectAction   = "action2";
            var actionResponses     = new Dictionary <GameObject, ActionResponsePair[]> {
                { first.MockObject, new [] { new ActionResponsePair("action", new[] { "response" }) } },
                { second.MockObject, new [] { new ActionResponsePair(frontObjectAction, new[] { frontObjectResponse }) } }
            };

            using (factory.Ordered()) {
                expectTouchWithinSprite(sprite.MockObject, true);
                expectTouchWithinSprite(sprite.MockObject, false);
                expectTouchWithinSprite(sprite.MockObject, true);
            }

            messagePromptCoordinator.hintWhenTouched(
                GameObject => {
            },
                sensor.MockObject,
                beforeEverything,
                actionResponses
                );

            messagePromptCoordinator.Update(beforeEverything);

            GameObject triggeredObject = null;

            messageBox.Expects.One.Method(_ => _.setMessage("")).With(Is.EqualTo(frontObjectResponse));
            messageBox.Expects.One.Method(_ => _.hide());
            messageBox.Expects.One.Method(_ => _.show());

            prompt.Expects.One.Method(_ => _.setText("")).With(Is.EqualTo(frontObjectAction));
            prompt.Expects.One.Method(_ => _.hide());
            prompt.Expects.One.Method(_ => _.show());

            messagePromptCoordinator.hintWhenTouched(
                gameObject => {
                triggeredObject = gameObject;
            },
                sensor.MockObject,
                afterMessageTime,
                actionResponses
                );

            messageBox.Expects.One.Method(_ => _.hide());
            messagePromptCoordinator.Update(afterMessageTime + MessagePromptCoordinator.promptTime + 0.1f);

            Assert.That(triggeredObject, Iz.EqualTo(second.MockObject));
        }
예제 #23
0
        public void MatchesInvocationWithSameNumberOfArgumentsAsMatcherHasValueMatchersAndValueMatchersMatch()
        {
            Matcher matcher = new ArgumentsMatcher(Is.EqualTo(arg1Value), Is.EqualTo(arg2Value));

            Assert.IsTrue(matcher.Matches(InvocationWithArguments(arg1Value, arg2Value)));
        }