public void ForPropertySet()
        {
            Expression <Func <int> > expression = () => myObject.Property;

            var invocationMatcher = InvocationMatcher.ForPropertySet(expression, 42);

            Assert.AreSame(myObject, invocationMatcher.Target);
            Assert.AreEqual(typeof(IMyObject).GetProperty("Property").GetSetMethod(), invocationMatcher.Method);
            CollectionAssert.AreEqual(new [] { 42 }, invocationMatcher.ParameterValueConstraints);
        }
        public void ForPropertySetWithIndex()
        {
            Expression <Func <string> > expression = () => myObject[Any <int> .Value];

            var invocationMatcher = InvocationMatcher.ForPropertySet(expression, Any <string> .Value);

            Assert.AreSame(myObject, invocationMatcher.Target);
            Assert.AreEqual(typeof(IMyObject).GetProperty("Item").GetSetMethod(), invocationMatcher.Method);
            CollectionAssert.AreEqual(new object[] { Any <int> .Value, Any <string> .Value }, invocationMatcher.ParameterValueConstraints);
        }
        public void ForGenericMethodCall()
        {
            Expression <Action> expression = () => myObject.GenericMethod("1", Any <string> .Value);

            var invocationMatcher = InvocationMatcher.ForMethodCall(expression);

            Assert.AreSame(myObject, invocationMatcher.Target);
            Assert.AreEqual(typeof(IMyObject).GetMethod("GenericMethod").MakeGenericMethod(new[] { typeof(string), typeof(string) }), invocationMatcher.Method);
            CollectionAssert.AreEqual(new object[] { "1", Any <string> .Value }, invocationMatcher.ParameterValueConstraints);
        }
Esempio 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));
        }
        public void ForMethodCall()
        {
            Expression <Action> expression = () => myObject.Method(42);

            var invocationMatcher = InvocationMatcher.ForMethodCall(expression);

            Assert.AreSame(myObject, invocationMatcher.Target);
            Assert.AreEqual(typeof(IMyObject).GetMethod("Method"), invocationMatcher.Method);
            CollectionAssert.AreEqual(new object[] { 42 }, invocationMatcher.ParameterValueConstraints);
        }
        public void MatchesWithParameterValueConstraints()
        {
            var invocationMatcher = new InvocationMatcher(myObject, typeof(IMyObject).GetMethod("Method"), new object[] { Any <int> .Value });

            Assert.IsTrue(invocationMatcher.Matches(CreateMethodInvocation(myObject, "Method", int.MinValue)));
            Assert.IsTrue(invocationMatcher.Matches(CreateMethodInvocation(myObject, "Method", 42)));
            Assert.IsTrue(invocationMatcher.Matches(CreateMethodInvocation(myObject, "Method", int.MaxValue)));

            Assert.IsFalse(invocationMatcher.Matches(CreateMethodInvocation(myObject, "Method", "42")));
        }
        public void Matches()
        {
            var invocationMatcher = new InvocationMatcher(myObject, typeof(IMyObject).GetMethod("Method"), new object[] { 42 });

            Assert.IsTrue(invocationMatcher.Matches(CreateMethodInvocation(myObject, "Method", 42)));

            Assert.IsFalse(invocationMatcher.Matches(CreateMethodInvocation(myObject, "Method", 43)));
            Assert.IsFalse(invocationMatcher.Matches(CreateMethodInvocation(myObject, "Method", 42, 0)));
            Assert.IsFalse(invocationMatcher.Matches(CreateMethodInvocation(new MyObject(), "Method", 42)));
            Assert.IsFalse(invocationMatcher.Matches(CreateMethodInvocation(myObject, "MethodWithReturnValue", 42)));
        }
        public void MatchesAnyInvocationOnDelegateDoesNotIncludesMethodsDeclaredOnObject()
        {
            var target = myDelegate;

            var objectMethodInvocation =
                new Invocation((IProxy)target.Target, typeof(object).GetMethod("ToString"), null, new object[0], null, 0);

            var invocationMatcher = InvocationMatcher.ForAnyInvocationOn(target);

            Assert.IsFalse(invocationMatcher.Matches(objectMethodInvocation));
        }
        public void ForMethodCallWithParameterValueConstraint()
        {
            Expression <Func <int> > expression = () => myObject.MethodWithReturnValue(Any <int> .Value.Matching(i => i % 2 == 0));

            var invocationMatcher = InvocationMatcher.ForMethodCall(expression);

            var parameterValueConstraint = invocationMatcher.ParameterValueConstraints[0];

            Assert.IsInstanceOf <MatchingPredicateValueConstraint <int> >(parameterValueConstraint);
            Assert.AreEqual(Any <int> .Value.Matching(i => i % 2 == 0).ToString(), parameterValueConstraint.ToString());
        }
        public void MatchesAnyInvocationOnTarget()
        {
            var target1 = new MyObject();
            var target2 = new MyObject();

            var invocationMatcher = InvocationMatcher.ForAnyInvocationOn(target1);

            Assert.IsTrue(invocationMatcher.Matches(CreateMethodInvocation(target1, "Method")));
            Assert.IsTrue(invocationMatcher.Matches(CreateMethodInvocation(target1, "MethodWithReturnValue")));

            Assert.IsFalse(invocationMatcher.Matches(CreateMethodInvocation(target2, "MethodWithReturnValue")));
        }
        public void ForPropertyGetWithIndex()
        {
            Expression <Func <string> > expression = () => myObject[Any <int> .Value];

            var invocationMatcher = InvocationMatcher.ForPropertyGet(expression);

            Assert.AreSame(myObject, invocationMatcher.Target);
            Assert.AreEqual(typeof(IMyObject).GetProperty("Item").GetGetMethod(), invocationMatcher.Method);

            var parameterValueConstraint = invocationMatcher.ParameterValueConstraints[0];

            Assert.IsInstanceOf <AnyValueConstraint <int> >(parameterValueConstraint);
        }
        public void MatchesGenericMethod()
        {
            var invocationMatcher =
                new InvocationMatcher(
                    myObject,
                    typeof(IMyObject).GetMethod("GenericMethod").MakeGenericMethod(new[] { typeof(string), typeof(int) }),
                    new object[] { "1", 1 });

            Assert.IsTrue(invocationMatcher.Matches(CreateGenericMethodInvocation(myObject, "GenericMethod", new[] { typeof(string), typeof(int) }, "1", 1)));

            Assert.IsFalse(invocationMatcher.Matches(CreateGenericMethodInvocation(myObject, "GenericMethod", new[] { typeof(int), typeof(string) }, 1, "1")));
            Assert.IsFalse(invocationMatcher.Matches(CreateGenericMethodInvocation(myObject, "GenericMethod", new[] { typeof(string), typeof(int) }, "2", 2)));
        }
        public void ForPropertyGetWithInvalidExpression()
        {
            var invalidExpressions =
                new Expression <Func <object> >[]
            {
                () => myObject.MethodWithReturnValue(42),
                () => myObject.Property + 1,
                () => myObject[1].ToLower(),
            };

            foreach (var expression in invalidExpressions)
            {
                Assert.Throws <ArgumentException>(() => InvocationMatcher.ForPropertyGet(expression));
            }
        }
        public void ForMethodCallWithInvalidExpression()
        {
            var invalidExpressions =
                new Expression <Func <object> >[]
            {
                () => myObject.MethodWithReturnValue(42) + 1,
                () => myObject.Property,
                () => myObject[0]
            };

            foreach (var expression in invalidExpressions)
            {
                Assert.Throws <ArgumentException>(() => InvocationMatcher.ForMethodCall(expression));
            }
        }
Esempio n. 15
0
 public IAssertInvocations ForPropertySet <T>(Expression <Func <T> > propertyExpression, T value)
 {
     return(Assert(InvocationMatcher.ForPropertySet(propertyExpression, value)));
 }
        public void ForMethodCallWithInvalidUsageOfAnyValueAsInterface()
        {
            Expression <Action> expression = () => myObject.Method(Any <int> .Value.AsInterface);

            Assert.Throws <ArgumentException>(() => InvocationMatcher.ForMethodCall(expression));
        }
Esempio n. 17
0
 public ISpecifyAction <T> PropertyGet <T>(Expression <Func <T> > propertyExpression)
 {
     return(ActionInvoked <T>(InvocationMatcher.ForPropertyGet(propertyExpression)));
 }
Esempio n. 18
0
 public ISpecifyAction PropertySet <T>(Expression <Func <T> > propertyExpression, ParameterValueConstraint <T> value)
 {
     return(ActionInvoked(InvocationMatcher.ForPropertySet(propertyExpression, value)));
 }
Esempio n. 19
0
 public ISpecifyAction PropertySet <T>(Expression <Func <T> > propertyExpression, T value)
 {
     return(ActionInvoked(InvocationMatcher.ForPropertySet(propertyExpression, value)));
 }
Esempio n. 20
0
 public ISpecifyActionForAny AnyInvocationOn(object target)
 {
     return(ActionInvoked(InvocationMatcher.ForAnyInvocationOn(target)));
 }
Esempio n. 21
0
 public ISpecifyAction EventRemove <TTarget, THandler>(TTarget target, string eventName, THandler handler)
 {
     return(ActionInvoked(InvocationMatcher.ForEventRemove(target, eventName, handler)));
 }
Esempio n. 22
0
 SpecifyAction <T> ActionInvoked <T>(InvocationMatcher invocationMatcher)
 {
     return(new SpecifyAction <T>(CreateExpectation(invocationMatcher, numberOfInvocationsConstraint, hasHigherPrecedence)));
 }
Esempio n. 23
0
 public MatchedInvocations(MatchedInvocations previousMatch, NumberOfInvocationsConstraint numberOfInvocationsConstraint, InvocationMatcher invocationMatcher, IInvocation[] matchingInvocations)
 {
     this.previousMatch = previousMatch;
     this.numberOfInvocationsConstraint = numberOfInvocationsConstraint;
     this.invocationMatcher             = invocationMatcher;
     this.matchingInvocations           = matchingInvocations;
 }
 public void ForEventAddOnNonExistingEvent()
 {
     Assert.Throws <ArgumentException>(() => InvocationMatcher.ForEventAdd(myObject, "Event2", Any <EventHandler> .Value));
 }
Esempio n. 25
0
 public ISpecifyAction <T> MethodCall <T>(Expression <Func <T> > methodCallExpression)
 {
     return(ActionInvoked <T>(InvocationMatcher.ForMethodCall(methodCallExpression)));
 }
Esempio n. 26
0
 public IAssertInvocations ForMethodCall(Expression <Action> methodCallExpression)
 {
     return(Assert(InvocationMatcher.ForMethodCall(methodCallExpression)));
 }
Esempio n. 27
0
 public IAssertInvocations ForMethodCall <T>(Expression <Func <T> > methodCallExpression)
 {
     return(Assert(InvocationMatcher.ForMethodCall(methodCallExpression)));
 }
Esempio n. 28
0
 public ISpecifyAction MethodCall(Expression <Action> methodCallExpression)
 {
     return(ActionInvoked(InvocationMatcher.ForMethodCall(methodCallExpression)));
 }
Esempio n. 29
0
 public IAssertInvocations ForEventRemove <TTarget, THandler>(TTarget target, string eventName, THandler handler)
 {
     return(Assert(InvocationMatcher.ForEventRemove(target, eventName, handler)));
 }
Esempio n. 30
0
 public IAssertInvocations ForPropertyGet<T>(Expression<Func<T>> propertyExpression)
 {
     return Assert(InvocationMatcher.ForPropertyGet(propertyExpression));
 }