Beispiel #1
0
        void IInvokable.Invoke(Invocation invocation)
        {
            switch (MockStyle)
            {
            case MockStyle.Default:
            case MockStyle.Transparent:
                try
                {
                    //call up the the factory to route this invocation
                    MockFactory.Dispatch(invocation);
                }
                catch (UnexpectedInvocationException)
                {
                    if (IgnoreUnexpectedInvocations)
                    {
                        //clear the factory expectation
                        MockFactory.ClearException();
                    }
                    else
                    {
                        throw;
                    }
                }
                break;

            case MockStyle.Stub:
            {
                if (MockFactory.HasExpectationForIgnoringIsActive(invocation))
                {
                    goto case MockStyle.Default;
                }

                // check whether we already have a value for this call
                object result;

                if (invocation.IsPropertySetAccessor)                                // remember values set in a property setter
                {
                    if (assignedPropertyResults.ContainsKey(invocation.MethodName))  //TODO: stubs don't support indexers! need Item[int] or Item[string,int]
                    {
                        assignedPropertyResults[invocation.MethodName] = invocation.Parameters[0];
                    }
                    else
                    {
                        assignedPropertyResults.Add(invocation.MethodName, invocation.Parameters[0]);
                    }

                    return;
                }
                else if (invocation.IsEventAccessor)
                {
                    ProcessEventHandlers(invocation);
                    return;
                }
                else if (invocation.IsPropertyGetAccessor && assignedPropertyResults.ContainsKey(invocation.MethodName))
                {
                    result = assignedPropertyResults[invocation.MethodName];
                }
                else if (rememberedMethodResults.ContainsKey(invocation.Method))
                {
                    result = rememberedMethodResults[invocation.Method];
                }
                else
                {
                    result = GetStubResult(invocation);
                    rememberedMethodResults.Add(invocation.Method, result);
                }

                if (result != Missing.Value)
                {
                    invocation.Result = result;
                }

                break;
            }

            case MockStyle.RecursiveStub:
            {
                if (MockFactory.HasExpectationFor(invocation))
                {
                    goto case MockStyle.Default;
                }

                object result;
                if (rememberedResults.ContainsKey(invocation.MethodName))
                {
                    result = rememberedResults[invocation.MethodName];
                }
                else
                {
                    result = GetStubResult(invocation);
                    rememberedResults.Add(invocation.MethodName, result);
                }

                if (result != Missing.Value)
                {
                    invocation.Result = result;
                }

                break;
            }
            }
        }