Ejemplo n.º 1
0
        /// <summary>
        /// This check the methods that were setup using the SetupResult.For()
        /// or LastCall.Repeat.Any() and that bypass the whole expectation model.
        /// </summary>
        public IExpectation GetRepeatableExpectation(object proxy, MethodInfo method, object[] args)
        {
            ProxyMethodPair pair = new ProxyMethodPair(proxy, method);

            if (repeatableMethods.ContainsKey(pair) == false)
            {
                return(null);
            }
            ExpectationsList list = repeatableMethods[pair];

            foreach (IExpectation expectation in list)
            {
                if (expectation.IsExpected(args))
                {
                    expectation.AddActualCall();
                    if (expectation.RepeatableOption == RepeatableOption.Never)
                    {
                        string errMsg = string.Format("{0} Expected #{1}, Actual #{2}.", expectation.ErrorMessage, expectation.Expected, expectation.ActualCallsCount);
                        ExpectationViolationException exception = new ExpectationViolationException(errMsg);
                        MockRepository.SetExceptionToBeThrownOnVerify(proxy, exception);
                        throw exception;
                    }
                    return(expectation);
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
 public void CtorValueReturnedInProperties()
 {
     object mockProxy = new object(); // mock for the mocked, he he
     ProxyMethodPair pair = new ProxyMethodPair(mockProxy, endsWith);
     Assert.AreSame(mockProxy, pair.Proxy);
     Assert.AreSame(endsWith, pair.Method);
 }
Ejemplo n.º 3
0
 public void NotEqualIfNotSameMethod()
 {
     ProxyInstance mockProxy = new ProxyInstance(null);
     ProxyMethodPair pair1 = new ProxyMethodPair(mockProxy, endsWith);
     ProxyMethodPair pair2 = new ProxyMethodPair(mockProxy, startsWith);
     Assert.AreNotEqual(pair1, pair2);
     Assert.AreNotEqual(pair2, pair1); //make sure that it works both ways
 }
Ejemplo n.º 4
0
 public void EqualsToAnotherProxy()
 {
     ProxyInstance mockProxy = new ProxyInstance(null);
     ProxyMethodPair pair1 = new ProxyMethodPair(mockProxy, endsWith);
     ProxyMethodPair pair2 = new ProxyMethodPair(mockProxy, endsWith);
     Assert.AreEqual(pair1, pair2);
     Assert.AreEqual(pair2, pair1); //make sure that it works both ways
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Determines whatever obj equals to this instance.
        /// ProxyMethodPairs are equal when they point to the same /instance/ of
        /// an object, and to the same method.
        /// </summary>
        /// <param name="obj">Obj.</param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            ProxyMethodPair other = obj as ProxyMethodPair;

            if (other == null)
            {
                return(false);
            }
            return(MockedObjectsEquality.Instance.Compare(other.proxy, proxy) == 0 &&
                   other.method == method);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Remove the all repeatable expectations for proxy.
 /// </summary>
 /// <param name="proxy">Mocked object.</param>
 public void RemoveAllRepeatableExpectationsForProxy(object proxy)
 {
     ProxyMethodPair[] keys = new ProxyMethodPair[repeatableMethods.Keys.Count];
     repeatableMethods.Keys.CopyTo(keys, 0);
     foreach (ProxyMethodPair pair in keys)
     {
         if (MockedObjectsEquality.Instance.Equals(pair.Proxy, proxy))
         {
             repeatableMethods.Remove(pair);
         }
     }
 }
Ejemplo n.º 7
0
        private bool TryReplaceRepeatableExpectation(MethodInfo method, IExpectation newExpectation, IExpectation oldExpectation, object proxy)
        {
            ProxyMethodPair pair = new ProxyMethodPair(proxy, method);

            if (repeatableMethods.ContainsKey(pair))
            {
                ExpectationsList expectationsList = repeatableMethods[pair];
                int indexOf = expectationsList.IndexOf(oldExpectation);
                if (indexOf != -1)
                {
                    expectationsList[indexOf] = newExpectation;
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Set the expectation so it can repeat any number of times.
        /// </summary>
        public void AddToRepeatableMethods(object proxy, MethodInfo method, IExpectation expectation)
        {
            //Just to get an error if I make a mistake and try to add a normal
            //method to the speical repeat method
            Debug.Assert(expectation.RepeatableOption != RepeatableOption.Normal);
            RemoveExpectation(expectation);
            ProxyMethodPair pair = new ProxyMethodPair(proxy, method);

            if (repeatableMethods.ContainsKey(pair) == false)
            {
                repeatableMethods.Add(pair, new ExpectationsList());
            }
            ExpectationsList expectationsList = repeatableMethods[pair];

            ExpectationNotOnList(expectationsList, expectation,
                                 MockRepository.IsStub(proxy));
            expectationsList.Add(expectation);
        }
Ejemplo n.º 9
0
 private bool TryReplaceRepeatableExpectation(MethodInfo method, IExpectation newExpectation, IExpectation oldExpectation, object proxy)
 {
     ProxyMethodPair pair = new ProxyMethodPair(proxy, method);
     if (repeatableMethods.ContainsKey(pair))
     {
         ExpectationsList expectationsList = repeatableMethods[pair];
         int indexOf = expectationsList.IndexOf(oldExpectation);
         if (indexOf != -1)
         {
             expectationsList[indexOf] = newExpectation;
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Remove the all repeatable expectations for proxy.
 /// </summary>
 /// <param name="proxy">Mocked object.</param>
 public void RemoveAllRepeatableExpectationsForProxy(object proxy)
 {
     ProxyMethodPair[] keys = new ProxyMethodPair[repeatableMethods.Keys.Count];
     repeatableMethods.Keys.CopyTo(keys, 0);
     foreach (ProxyMethodPair pair in keys)
     {
         if (MockedObjectsEquality.Instance.Equals(pair.Proxy, proxy))
             repeatableMethods.Remove(pair);
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// This check the methods that were setup using the SetupResult.For()
 /// or LastCall.Repeat.Any() and that bypass the whole expectation model.
 /// </summary>
 public IExpectation GetRepeatableExpectation(object proxy, MethodInfo method, object[] args)
 {
     ProxyMethodPair pair = new ProxyMethodPair(proxy, method);
     if (repeatableMethods.ContainsKey(pair) == false)
         return null;
     ExpectationsList list = repeatableMethods[pair];
     foreach (IExpectation expectation in list)
     {
         if (expectation.IsExpected(args))
         {
             expectation.AddActualCall();
             if (expectation.RepeatableOption == RepeatableOption.Never)
             {
                 string errMsg = string.Format("{0} Expected #{1}, Actual #{2}.", expectation.ErrorMessage, expectation.Expected, expectation.ActualCallsCount);
                 ExpectationViolationException exception = new ExpectationViolationException(errMsg);
                 MockRepository.SetExceptionToBeThrownOnVerify(proxy, exception);
                 throw exception;
             }
             return expectation;
         }
     }
     return null;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Set the expectation so it can repeat any number of times.
 /// </summary>
 public void AddToRepeatableMethods(object proxy, MethodInfo method, IExpectation expectation)
 {
     //Just to get an error if I make a mistake and try to add a normal
     //method to the speical repeat method
     Debug.Assert(expectation.RepeatableOption != RepeatableOption.Normal);
     RemoveExpectation(expectation);
     ProxyMethodPair pair = new ProxyMethodPair(proxy, method);
     if (repeatableMethods.ContainsKey(pair) == false)
         repeatableMethods.Add(pair, new ExpectationsList());
     ExpectationsList expectationsList = repeatableMethods[pair];
     ExpectationNotOnList(expectationsList, expectation,
         MockRepository.IsStub(proxy));
     expectationsList.Add(expectation);
 }
Ejemplo n.º 13
0
 public void NotEqualToNull()
 {
     ProxyInstance mockProxy = new ProxyInstance(null);
     ProxyMethodPair pair1 = new ProxyMethodPair(mockProxy, endsWith);
     Assert.IsFalse(pair1.Equals(null));
 }
Ejemplo n.º 14
0
 public void NotEqualIfNotSameMethodAndNotSameProxy()
 {
     ProxyInstance mockProxy1 = new ProxyInstance(null);
     ProxyInstance mockProxy2 = new ProxyInstance(null);
     ProxyMethodPair pair1 = new ProxyMethodPair(mockProxy1, this.endsWith);
     ProxyMethodPair pair2 = new ProxyMethodPair(mockProxy2, this.startsWith);
     Assert.NotEqual(pair1, pair2);
     Assert.NotEqual(pair2, pair1); //make sure that it works both ways
 }