public void When_a_trace_is_dumped_it_should_return_right_traces()
        {
            var tracingService = new XrmFakedTracingService();

            var trace1 = "This is one trace";
            var trace2 = "This is a second trace";

            tracingService.Trace(trace1);
            tracingService.Trace(trace2);

            Assert.Equal(tracingService.DumpTrace(), trace1 + trace2);
        }
        /// <summary>
        /// Executes a code activity passing the primary entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public IDictionary <string, object> ExecuteCodeActivity <T>(XrmFakedWorkflowContext wfContext, Dictionary <string, object> inputs, T instance = null) where T : CodeActivity, new()
        {
            WorkflowInvoker invoker = null;
            WorkflowInstanceExtensionManager mngr = null;
            string sDebug = "";

            try
            {
                sDebug = "Creating instance..." + System.Environment.NewLine;
                if (instance == null)
                {
                    instance = new T();
                }
                invoker = new WorkflowInvoker(instance);
                sDebug += "Invoker created" + System.Environment.NewLine;
                sDebug += "Adding extensions..." + System.Environment.NewLine;
                invoker.Extensions.Add <ITracingService>(() =>
                {
                    _tracingService = new XrmFakedTracingService();
                    return(_tracingService);
                });
                invoker.Extensions.Add <IWorkflowContext>(() =>
                {
                    return(wfContext);
                });
                invoker.Extensions.Add <IOrganizationServiceFactory>(() => {
                    var fakedServiceFactory = A.Fake <IOrganizationServiceFactory>();
                    A.CallTo(() => fakedServiceFactory.CreateOrganizationService(A <Guid?> ._))
                    .ReturnsLazily((Guid? g) =>
                    {
                        return(GetOrganizationService());
                    });
                    return(fakedServiceFactory);
                });

                sDebug += "Adding extensions...ok." + System.Environment.NewLine;
                sDebug += "Invoking activity..." + System.Environment.NewLine;
                return(invoker.Invoke(inputs));
            }
            catch (TypeLoadException tlex)
            {
                var typeName = tlex.TypeName != null ? tlex.TypeName : "(null)";
                throw new TypeLoadException("When loading type: " + typeName + "." + tlex.Message + "in domain directory: " + AppDomain.CurrentDomain.BaseDirectory + "Debug=" + sDebug);
            }
        }
        protected IServiceProvider GetFakedServiceProvider(XrmFakedPluginExecutionContext plugCtx)
        {
            var fakedServiceProvider = A.Fake <IServiceProvider>();

            A.CallTo(() => fakedServiceProvider.GetService(A <Type> ._))
            .ReturnsLazily((Type t) =>
            {
                if (t.Equals(typeof(IOrganizationService)))
                {
                    //Return faked or real organization service
                    return(GetOrganizationService());
                }
                else if (t.Equals(typeof(ITracingService)))
                {
                    _tracingService = new XrmFakedTracingService();
                    return(_tracingService);
                }
                else if (t.Equals(typeof(IPluginExecutionContext)))
                {
                    return(GetFakedPluginContext(plugCtx));
                }
                else if (t.Equals(typeof(IExecutionContext)))
                {
                    return(GetFakedExecutionContext(plugCtx));
                }
                else if (t.Equals(typeof(IOrganizationServiceFactory)))
                {
                    var fakedServiceFactory = A.Fake <IOrganizationServiceFactory>();
                    A.CallTo(() => fakedServiceFactory.CreateOrganizationService(A <Guid?> ._))
                    .ReturnsLazily((Guid? g) =>
                    {
                        return(GetOrganizationService());
                    });
                    return(fakedServiceFactory);
                }
                else if (t.Equals(typeof(IServiceEndpointNotificationService)))
                {
                    var fakedNotificationService = A.Fake <IServiceEndpointNotificationService>();
                    return(fakedNotificationService);
                }
                throw new PullRequestException("The specified service type is not supported");
            });

            return(fakedServiceProvider);
        }