Пример #1
0
        public static string TheUserId()
        {
            const string userid         = "userId";
            var          sessionContext = Pleasure.MockStrictAsObject <ISessionContext>(mock => mock.SetupGet(r => r.UserId).Returns(userid));

            IoCFactory.Instance.StubTryResolve(sessionContext);
            return(userid);
        }
        static void Run(Action <EntityFrameworkUnitOfWork, Mock <DbContext> > action, bool isOpen = true)
        {
            var    dbContext        = Pleasure.Mock <DbContext>();
            string connectionString = ConfigurationManager.ConnectionStrings["IncRealEFDb"].ConnectionString;
            var    sessionFactory   = Pleasure.MockStrictAsObject <IEntityFrameworkSessionFactory>(mock => mock.Setup(r => r.Open(connectionString)).Returns(dbContext.Object));
            var    unitOfWork       = new EntityFrameworkUnitOfWork(sessionFactory, IsolationLevel.ReadCommitted, connectionString);

            if (isOpen)
            {
                unitOfWork.Open();
            }

            action(unitOfWork, dbContext);
        }
Пример #3
0
        static Context_dispatcher_controller()
        {
            dispatcher = Pleasure.Mock <IDispatcher>();

            IoCFactory.Instance.StubTryResolve(dispatcher.Object);
            controller = new FakeDispatcher();

            requestBase = Pleasure.Mock <HttpRequestBase>(mock =>
            {
                mock.SetupGet(r => r.Headers).Returns(new NameValueCollection {
                    { "X-Requested-With", "XMLHttpRequest" }
                });
            });

            responseBase = Pleasure.MockStrict <HttpResponseBase>();
            controller.ControllerContext = new ControllerContext(Pleasure.MockStrictAsObject <HttpContextBase>(mock =>
            {
                mock.SetupGet(r => r.Request).Returns(requestBase.Object);
                mock.SetupGet(r => r.Response).Returns(responseBase.Object);
            }), new RouteData(), controller);
            controller.ValueProvider = Pleasure.MockStrictAsObject <IValueProvider>(mock => mock.Setup(r => r.GetValue(Pleasure.MockIt.IsAny <string>())).Returns(new ValueProviderResult(string.Empty, string.Empty, Thread.CurrentThread.CurrentCulture)));
        }
        protected static void Establish(Type[] types = null, bool isAjax = true)
        {
            typeof(DispatcherControllerBase).GetField("types", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, new List <Type>());

            dispatcher = Pleasure.Mock <IDispatcher>();
            IoCFactory.Instance.StubTryResolve(dispatcher.Object);
            controller = new FakeDispatcher();

            var requestBase = Pleasure.MockAsObject <HttpRequestBase>(mock =>
            {
                if (isAjax)
                {
                    mock.SetupGet(r => r.Headers).Returns(new NameValueCollection {
                        { "X-Requested-With", "XMLHttpRequest" }
                    });
                }

                mock.SetupGet(r => r.Form).Returns(new NameValueCollection()
                {
                    { "[0].Name", "Value" },
                    { "[1].Name", "Value" },
                });
            });

            controller.ControllerContext = new ControllerContext(Pleasure.MockStrictAsObject <HttpContextBase>(mock => mock.SetupGet(r => r.Request).Returns(requestBase)), new RouteData(), controller);
            controller.ValueProvider     = Pleasure.MockStrictAsObject <IValueProvider>(mock => mock.Setup(r => r.GetValue(Pleasure.MockIt.IsAny <string>())).Returns(new ValueProviderResult(string.Empty, string.Empty, Thread.CurrentThread.CurrentCulture)));

            var modelBinderDictionary = new ModelBinderDictionary();
            var modelBinder           = Pleasure.MockAsObject <IModelBinder>(mock => mock.Setup(r => r.BindModel(Pleasure.MockIt.IsAny <ControllerContext>(),
                                                                                                                 Pleasure.MockIt.IsAny <ModelBindingContext>())));

            foreach (var type in types.Recovery(new Type[] { }))
            {
                modelBinderDictionary.Add(type, modelBinder);
            }
            controller.SetValue("Binders", modelBinderDictionary);
        }
Пример #5
0
        static void Run(Action <NhibernateUnitOfWork, Mock <ISession>, Mock <ITransaction> > action, bool isOpen = true, bool isDispose = false)
        {
            var transaction = Pleasure.Mock <ITransaction>();

            string dbConnection = Pleasure.Generator.Invent <SqlConnection>().ConnectionString;
            var    session      = Pleasure.Mock <ISession>(mock =>
            {
                mock.Setup(r => r.SessionFactory).Returns(Pleasure.MockAsObject <ISessionFactoryImplementor>(r => r.SetupGet(implementor => implementor.CurrentSessionContext)
                                                                                                             .Returns(Pleasure.MockAsObject <CurrentSessionContext>())));
                mock.Setup(r => r.BeginTransaction(IsolationLevel.RepeatableRead)).Returns(transaction.Object);
            });

            var nhibernateUnit = new NhibernateUnitOfWork(Pleasure.MockStrictAsObject <INhibernateSessionFactory>(mock =>
            {
                mock.Setup(r => r.Open(dbConnection)).Returns(session.Object);
            }), dbConnection, IsolationLevel.RepeatableRead);

            if (isOpen)
            {
                nhibernateUnit.Open();
            }

            action(nhibernateUnit, session, transaction);
        }