コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #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));
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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")));
        }
コード例 #7
0
        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)));
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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());
        }
コード例 #10
0
        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")));
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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)));
        }
コード例 #13
0
        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));
            }
        }
コード例 #14
0
        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));
            }
        }
コード例 #15
0
 public IAssertInvocations ForPropertySet <T>(Expression <Func <T> > propertyExpression, T value)
 {
     return(Assert(InvocationMatcher.ForPropertySet(propertyExpression, value)));
 }
コード例 #16
0
        public void ForMethodCallWithInvalidUsageOfAnyValueAsInterface()
        {
            Expression <Action> expression = () => myObject.Method(Any <int> .Value.AsInterface);

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