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);
            }
        }
Пример #2
0
        public void ExpectAnyInvocationOn()
        {
            Expect.AnyInvocationOn(myObject);

            myObject.MyMethod(1);
            myObject.MyMethod(2);
            myObject.MyMethod(3);

            myObject.MyProperty = 1;
            myObject.MyEvent   += delegate { };

            int outValue;

            myObject.MyMethodWithOutParameter(0, out outValue);

            myObject.MyGenericMethod(1.0);
        }
Пример #3
0
        public void ForStub()
        {
            var myObject = Mock.Interface <IMyObject>();

            Expect.PropertyGet(() => myObject.MyProperty).Returns(41);
            Expect.AnyInvocationOn(myObject);


            myObject.MyMethod(1);
            myObject.MyMethod(2);
            myObject.MyMethod(3);

            myObject.MyGenericMethod("hello world");
            myObject.MyGenericMethod("Hello World!!!");
            myObject.MyGenericMethod(4311077043);

            myObject.MyProperty = (int)myObject.MyProperty + 1;

            var eventHandler = (EventHandler) delegate { };

            myObject.MyEvent -= eventHandler;
            myObject.MyEvent += eventHandler;

            AssertExpectations.
            IsMetForCallTo.
            MethodCall(() => myObject.MyMethod(Any <int> .Value)).
            MethodCall(() => myObject.MyGenericMethod(Any <string> .Value.Matching(s => s.StartsWith("Hel")))).
            PropertyGet(() => myObject.MyProperty).
            PropertySet(() => myObject.MyProperty, 42).
            EventAdd(myObject, "MyEvent", Any <EventHandler> .Value).
            EventRemove(myObject, "MyEvent", Any <EventHandler> .Value);


            var ex = Assert.Throws <ExpectationsException>(() => AssertExpectations.IsMetForCallTo.MethodCall(() => myObject.MyMethod(4)));

            Assert.That(ex.Message, Is.StringStarting("Wrong number of invocations for 'myObject.MyMethod(4)', expected 1..* actual 0:"));
        }