コード例 #1
0
		public void DefaultReturnValue()
		{
			Assert.Null(ReturnValueUtil.DefaultValue(typeof (string),null));
			Assert.Equal(0, ReturnValueUtil.DefaultValue(typeof (int),null));
			Assert.Equal((short) 0, ReturnValueUtil.DefaultValue(typeof (short),null));
			Assert.Equal((char) 0, ReturnValueUtil.DefaultValue(typeof (char),null));
			Assert.Equal(0L, ReturnValueUtil.DefaultValue(typeof (long),null));
			Assert.Equal(0f, ReturnValueUtil.DefaultValue(typeof (float),null));
			Assert.Equal(0d, ReturnValueUtil.DefaultValue(typeof (double),null));
			Assert.Equal(TestEnum.DefaultValue, ReturnValueUtil.DefaultValue(typeof (TestEnum),null));
		}
コード例 #2
0
        /// <summary>
        /// Add a method call for this state' mock.
        /// </summary>
        /// <param name="invocation">The invocation for this method</param>
        /// <param name="method">The method that was called</param>
        /// <param name="args">The arguments this method was called with</param>
        protected override object DoMethodCall(IInvocation invocation, MethodInfo method, params object[] args)
        {
            IExpectation expectation = repository.Replayer.GetRecordedExpectationOrNull(proxy, method, args);

            if (expectation != null)
            {
                RhinoMocks.Logger.LogReplayedExpectation(invocation, expectation);
                return(expectation.ReturnOrThrow(invocation, args));
            }
            else
            {
                RhinoMocks.Logger.LogUnexpectedMethodCall(invocation, "Dynamic Mock: Unexpected method call ignored");
                return(ReturnValueUtil.DefaultValue(method.ReturnType, invocation));
            }
        }
コード例 #3
0
        /// <summary>
        /// Add a method call for this state' mock.
        /// </summary>
        /// <param name="invocation">The invocation for this method</param>
        /// <param name="method">The method that was called</param>
        /// <param name="args">The arguments this method was called with</param>
        public object MethodCall(IInvocation invocation, MethodInfo method, params object[] args)
        {
            try
            {
                AssertPreviousMethodIsClose();
                repository.lastMockedObject   = mockedObject;
                MockRepository.lastRepository = repository;
                IExpectation expectation;

                // Has the Arg class been used?
                if (ArgManager.HasBeenUsed)
                {
                    expectation = expectationBuilder.BuildParamExpectation(invocation, method);
                }
                else
                {
                    expectation = expectationBuilder.BuildDefaultExpectation(invocation, method, args, GetDefaultCallCountRangeExpectation);
                }
                RhinoMocks.Logger.Log(string.Format("{0} -> {1} ", expectation.ErrorMessage, expectation.GetType()));
                repository.Recorder.Record(mockedObject, method, expectation);
                LastExpectation = expectation;
                methodCallsCount++;
                RhinoMocks.Logger.LogRecordedExpectation(invocation, expectation);
                object returnValue;
                if (TryCreateReturnValue(expectation, out returnValue))
                {
                    return(returnValue);
                }
                return(ReturnValueUtil.DefaultValue(method.ReturnType, invocation));
            }
            finally
            {
                // Consume the Arg constraints only once, and reset it after each call.
                // this is in the finally block to make sure that an exeption does not
                // make subsequent unit tests fail.
                ArgManager.Clear();
            }
        }