public void FailureToMeetExpectationsInScopeThrowsDescribingException()
        {
            var expectationScope = new ExpectationScope();

            var myObject1 = Mock.Interface <IMyObject>(expectationScope);
            var myObject2 = Mock.Interface <IMyObject>(expectationScope);
            var myObject3 = Mock.Interface <IMyObject>(expectationScope);

            Expect.Once.MethodCall(() => myObject1.MyMethod(1));
            Expect.Once.MethodCall(() => myObject2.MyMethod(2));

            using (expectationScope.BeginOrdered())
            {
                Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(3));
                Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(Any <int> .Value.Matching(i => i > 10)));

                using (expectationScope.BeginUnordered())
                {
                    Expect.AtLeastOnce.MethodCall(() => myObject2.MyMethod(4));
                    Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(5));
                }
            }

            Expect.AnyInvocationOn(myObject3);


            myObject1.MyMethod(1);
            myObject2.MyMethod(2);

            myObject1.MyMethod(3);
            myObject1.MyMethod(3);
            myObject1.MyMethod(3);

            try
            {
                myObject1.MyMethod(4);
            }
            catch (ExpectationsException ex)
            {
                Assert.AreEqual(
                    "Unexpected invocation 'myObject.MyMethod(4)', expected:\r\n" +
                    "\r\n" +
                    "(invoked: 1 of 1) myObject.MyMethod(1)\r\n" +
                    "(invoked: 1 of 1) myObject2.MyMethod(2)\r\n" +
                    "In order {\r\n" +
                    "  (invoked: 3 of 1..*) myObject.MyMethod(3)\r\n" +
                    "  (invoked: 0 of 1..*) myObject.MyMethod(Any<Int32>.Value.Matching(i => (i > 10)))\r\n" +
                    "  Unordered {\r\n" +
                    "    (invoked: 0 of 1..*) myObject2.MyMethod(4)\r\n" +
                    "    (invoked: 0 of 1..*) myObject.MyMethod(5)\r\n" +
                    "  }\r\n" +
                    "}\r\n" +
                    "(invoked: 0 of *) myObject3.*\r\n" +
                    "\r\n" +
                    "Unexpected invocations:\r\n" +
                    "  myObject.MyMethod(4)\r\n" +
                    "\r\n",
                    ex.Message);
            }
        }
        public void SetUp()
        {
            expectationScope = new ExpectationScope();

            invocation1 = new Invocation();
            invocation2 = new Invocation();
            invocation3 = new Invocation();
        }
Exemple #3
0
        static IExpectationScope GetOrderedExpectationScope(IEnumerable <MatchedInvocations> matches)
        {
            var expectationScope = new ExpectationScope();

            using (expectationScope.BeginOrdered())
            {
                foreach (var match in matches)
                {
                    AddExpectation(expectationScope, match.invocationMatcher, match.numberOfInvocationsConstraint);
                }
            }

            return(expectationScope);
        }
Exemple #4
0
        public void ForScope()
        {
            var expectationScope = new ExpectationScope();

            var myObject1 = Mock.Interface <IMyObject>(expectationScope);
            var myObject2 = Mock.Interface <IMyObject>(expectationScope);

            Expect.Once.MethodCall(() => myObject1.MyMethod(1));
            Expect.Once.MethodCall(() => myObject2.MyMethod(2));

            myObject1.MyMethod(1);
            myObject2.MyMethod(2);

            AssertExpectations.IsMetFor(expectationScope);
        }
Exemple #5
0
        public void ForScope()
        {
            var expectationScope = new ExpectationScope();

            var myObject1 = Mock.Interface <IMyObject>(expectationScope);
            var myObject2 = Mock.Interface <IMyObject>(expectationScope);

            Expect.Once.MethodCall(() => myObject1.MyMethod(1));
            Expect.Once.MethodCall(() => myObject2.MyMethod(2));

            myObject1.MyMethod(1);
            myObject2.MyMethod(2);

            AssertInvocationsWasMade.MatchingExpectationsFor(expectationScope);
        }
        public void TestPersist()
        {
            //Create mocks
            var expectationScope = new ExpectationScope();
            IUserGateway mockGateway = Mock.Interface<IUserGateway>(expectationScope);
            IUserValidator mockValidator = Mock.Interface<IUserValidator>(expectationScope);

            //Create user
            User user = new User();

            //Expectations
            Expect.Once.MethodCall(() => mockValidator.Validate(user)).Returns(true);
            Expect.Once.MethodCall(() => mockGateway.Persist(user)).Returns(true);

            //Assign gateway
            user.Gateway = mockGateway;

            //Test method
            Assert.AreEqual(true, user.Persist(mockValidator));

            AssertExpectations.IsMetFor(expectationScope);
        }
        public void MeetExpectationsInScope()
        {
            var expectationScope = new ExpectationScope();

            var myObject1 = Mock.Interface <IMyObject>(expectationScope);
            var myObject2 = Mock.Interface <IMyObject>(expectationScope);

            Expect.Once.MethodCall(() => myObject1.MyMethod(1));
            Expect.Once.MethodCall(() => myObject2.MyMethod(2));

            using (expectationScope.BeginOrdered())
            {
                Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(3));
                Expect.Between(0, 3).MethodCall(() => myObject1.MyGenericMethod("WILL PROBABLY NEVER BE INVOKED"));
                Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(Any <int> .Value.Matching(i => i > 10)));

                using (expectationScope.BeginUnordered())
                {
                    Expect.AtLeastOnce.MethodCall(() => myObject2.MyMethod(4));
                    Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(5));
                }
            }

            myObject1.MyMethod(1);
            myObject2.MyMethod(2);

            myObject1.MyMethod(3);
            myObject1.MyMethod(3);

            myObject1.MyMethod(13);
            myObject1.MyMethod(12);
            myObject1.MyMethod(11);

            myObject1.MyMethod(5);
            myObject2.MyMethod(4);

            AssertExpectations.IsMetFor(expectationScope);
        }