public void GetFromTarget()
        {
            var invocationInterceptor = new MockInvocationInterceptor(new TestExpectationScope());
            var target = new Target(invocationInterceptor);

            Assert.AreSame(invocationInterceptor, MockInvocationInterceptor.GetFromTarget(target));
        }
Ejemplo n.º 2
0
        static Expectation CreateExpectation(InvocationMatcher invocationMatcher, NumberOfInvocationsConstraint numberOfInvocationsConstraint, bool hasHigherPrecedence)
        {
            var expectation = new Expectation(invocationMatcher, numberOfInvocationsConstraint);

            MockInvocationInterceptor.GetFromTarget(invocationMatcher.Target).AddExpectation(expectation, hasHigherPrecedence);

            return(expectation);
        }
        public void AddExpectation()
        {
            var invocationInterceptor = new MockInvocationInterceptor(new TestExpectationScope());

            var expectation = new TestExpectation();

            invocationInterceptor.AddExpectation(expectation, false);

            Assert.AreSame(expectation, ((TestExpectationScope)invocationInterceptor.ExpectationScope).AddedExpectation);
        }
Ejemplo n.º 4
0
        IAssertInvocations Assert(InvocationMatcher invocationMatcher)
        {
            var invocationHistory = MockInvocationInterceptor.GetFromTarget(invocationMatcher.Target).ExpectationScope.InvocationHistory;
            var matchingInvocations = invocationHistory.Invocations.Where(invocationMatcher.Matches).ToArray();

            if (!numberOfInvocationsConstraint.Matches(matchingInvocations.Length))
                throw new ExpectationsException(invocationHistory, "Wrong number of invocations for '{0}', expected {1} actual {2}:", invocationMatcher, numberOfInvocationsConstraint, matchingInvocations.Length);
       
            return new AssertInvocations(new MatchedInvocations(previousMatch, numberOfInvocationsConstraint, invocationMatcher, matchingInvocations));
        }
Ejemplo n.º 5
0
        public static T CreateMock <T>(bool tracked = true)
            where T : class
        {
            var state       = new MockState(typeof(T));
            var interceptor = new MockInvocationInterceptor(state);
            var mock        = proxyGenerator.CreateInterfaceProxyWithoutTarget <T>(interceptor);

            if (tracked)
            {
                statesByMock.Add(mock, state);
            }
            return(mock);
        }
        public void OnInvocation()
        {
            var expectationScope      = new TestExpectationScope();
            var invocationInterceptor = new MockInvocationInterceptor(expectationScope);
            var target     = new Target(invocationInterceptor);
            var invocation = CreateMethodInvocation <IExpectationScope>(target, "Add", new[] { typeof(IExpectation), typeof(bool) }, new object[2]);


            Assert.Throws <ExpectationsException>(() => invocationInterceptor.OnInvocation(invocation));


            expectationScope.CanMeet = true;
            invocationInterceptor.OnInvocation(invocation);

            Assert.IsTrue(expectationScope.HasBeenMet);
        }
        public void OnInvocationForObjectMethod()
        {
            var baseObject = new BaseObject();

            var expectationScope      = new TestExpectationScope();
            var invocationInterceptor = new MockInvocationInterceptor(expectationScope);
            var target = new Target(baseObject, invocationInterceptor);

            var invocation      = CreateMethodInvocation <object>(target, "ToString");
            int invocationCount = 0;

            baseObject.ToStringCallback = () => (++invocationCount).ToString();


            invocationInterceptor.OnInvocation(invocation);

            Assert.AreEqual("1", invocation.ReturnValue);
            Assert.AreEqual(1, invocationCount);
        }
Ejemplo n.º 8
0
        public static void MatchingExpectationsFor(object target)
        {
            var mockInvocationInterceptor = MockInvocationInterceptor.GetFromTarget(target);

            AssertExpectationScopeIsMet(mockInvocationInterceptor.ExpectationScope);
        }
 public void GetFromTargetProxyWithNonMockInvocationInterceptor()
 {
     Assert.Throws <ArgumentException>(() => MockInvocationInterceptor.GetFromTarget(new Target(new OtherInvocationInterceptor())));
 }
 public void GetFromTargetNonProxy()
 {
     Assert.Throws <ArgumentException>(() => MockInvocationInterceptor.GetFromTarget(new object()));
 }