public MockUrl StubAction(string url) { this.httpContext .SetupGet(r => r.Response) .Returns(Pleasure.MockAsObject <HttpResponseBase>(mock1 => mock1.Setup(r => r.ApplyAppPathModifier(Pleasure.MockIt.IsStrong(url))).Returns(url))); return(this); }
/// <summary> /// </summary> /// <param name="controller">original controller</param> /// <param name="dispatcher">mock of IDispatcher</param> MockController(TController controller, Mock <IDispatcher> dispatcher) { this.dispatcher = dispatcher; originalController = controller; httpContext = Pleasure.Mock <HttpContextBase>(); // preparing RouteData for ControllerContext var routeData = new RouteData(); routeData.Values.Add("Action", "Action"); routeData.Values.Add("Controller", "Controller"); // replacing original controller Context with Mock objects originalController.ControllerContext = new ControllerContext(httpContext.Object, routeData, Pleasure.Mock <ControllerBase>().Object); // preparing routes for UrlHelper var routes = new RouteCollection(); routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // replacing original controller UrlHelper with Mock objects originalController.Url = new UrlHelper(new RequestContext(httpContext.Object, routeData), routes); // replace MVC ViewEngines with Mock objects AddViewEngine(); }
static void StubInit(CachingInit cachingInit, Action <Mock <ICachedProvider> > action) { var mockProvider = Pleasure.Mock(action); if (cachingInit.Provider != null) { try { mockProvider = Mock.Get(cachingInit.Provider); action(mockProvider); } ////ncrunch: no coverage start catch (Exception) { mockProvider = Pleasure.Mock(action); } ////ncrunch: no coverage end } cachingInit.WithProvider(mockProvider.Object); }
void AddViewEngine() { this.view = Pleasure.Mock <IView>(); this.viewEngine = Pleasure.Mock <IViewEngine>(); this.viewEngine.Setup(r => r.FindPartialView(Pleasure.MockIt.IsAny <ControllerContext>(), Pleasure.MockIt.IsAny <string>(), Pleasure.MockIt.IsAny <bool>())).Returns(new ViewEngineResult(this.view.Object, this.viewEngine.Object)); ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(this.viewEngine.Object); }
public MockUrl StubRequest(Action <Mock <HttpRequestBase> > action) { var request = Pleasure.Mock <HttpRequestBase>(); action(request); this.httpContext.SetupGet(r => r.Request).Returns(request.Object); return(this); }
protected MockMessage(TMessage instanceMessage) { this.repository = Pleasure.Mock <IRepository>(); IoCFactory.Instance.StubTryResolve(this.repository.Object); this.eventBroker = Pleasure.MockStrict <IEventBroker>(); IoCFactory.Instance.StubTryResolve(this.eventBroker.Object); Original = instanceMessage; }
public MockMessage <TMessage, TResult> StubNotEmptyQuery <TEntity>(OrderSpecification <TEntity> orderSpecification = null, Specification <TEntity> whereSpecification = null, FetchSpecification <TEntity> fetchSpecification = null, PaginatedSpecification paginatedSpecification = null, int countEntity = 1) where TEntity : class, IEntity, new() { var entities = Pleasure.ToList <TEntity>(); for (int i = 0; i < countEntity; i++) { entities.Add(Pleasure.Generator.Invent <TEntity>()); } return(StubQuery(orderSpecification, whereSpecification, fetchSpecification, paginatedSpecification, entities.ToArray())); }
MockHtmlHelper() { textWriter = Pleasure.Mock <TextWriter>(); var viewDataDictionary = new ViewDataDictionary <TModel>(); viewContext = Pleasure.Mock <ViewContext>(mock => { mock.Setup(r => r.ClientValidationEnabled).Returns(true); mock.SetupGet(r => r.Writer).Returns(textWriter.Object); mock.SetupGet(r => r.ViewData).Returns(viewDataDictionary); }); viewDataContainer = Pleasure.Mock <IViewDataContainer>(mock => mock.SetupGet(r => r.ViewData).Returns(viewDataDictionary)); }
public static MockController <TController> When(params object[] ctorArgs) { var dispatcher = Pleasure.Mock <IDispatcher>(); IoCFactory.Instance.StubTryResolve(dispatcher.Object); var controller = (TController)Activator.CreateInstance(typeof(TController), ctorArgs.ToArray()); var res = new MockController <TController>(controller, dispatcher); res.httpContext.SetupGet(r => r.Request.Headers).Returns(new NameValueCollection { { "X-Requested-With", "XMLHttpRequest" } }); return(res); }
public static MockController <TController> When(params object[] ctorArgs) { var dispatcher = Pleasure.Mock <IDispatcher>(); var listCtors = new List <object> { dispatcher.Object }; if (ctorArgs.Length > 0) { listCtors.AddRange(ctorArgs); } var controller = (TController)Activator.CreateInstance(typeof(TController), listCtors.ToArray()); var res = new MockController <TController>(controller, dispatcher); res.httpContext.SetupGet(r => r.Request.Headers).Returns(new NameValueCollection { { "X-Requested-With", "XMLHttpRequest" } }); return(res); }
public MockUrl() { var routeData = new RouteData(); routeData.Values.Add("Action", "Action"); routeData.Values.Add("Controller", "Controller"); this.httpContext = Pleasure.Mock <HttpContextBase>(mock => { var httpRequestBase = Pleasure.MockAsObject <HttpRequestBase>(mock1 => mock1.SetupGet(r => r.ApplicationPath).Returns("/")); mock.SetupGet(r => r.Request).Returns(httpRequestBase); }); var requestContext = new RequestContext(this.httpContext.Object, routeData); var routes = new RouteCollection(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); Original = new UrlHelper(requestContext, routes); }
protected MockMessage(TMessage instanceMessage) { this.unitOfWork = Pleasure.MockStrictAsObject <IUnitOfWork>( mock => { mock.Setup(x => x.GetSession()).Returns(Pleasure.Generator.String()); mock.Setup(x => x.Open()); }); instanceMessage.Setting = new MessageExecuteSetting(); instanceMessage.Setting.SetValue("unitOfWork", unitOfWork); this.repository = Pleasure.Mock <IRepository>(mock => mock.Setup(r => r.SetProvider(unitOfWork))); IoCFactory.Instance.StubTryResolve(this.repository.Object); this.eventBroker = Pleasure.MockStrict <IEventBroker>(); IoCFactory.Instance.StubTryResolve(this.eventBroker.Object); this.dispatcher = Pleasure.MockStrict <IDispatcher>(mock => { mock.StubPush <CommandBase>(@base => { bool isAny = false; foreach (var action in this.actions) { try { action(@base); isAny = true; } catch (Exception) { } } isAny.ShouldBeTrue(); }); }); IoCFactory.Instance.StubTryResolve(this.dispatcher.Object); instanceMessage.Setting.SetValue("outerDispatcher", this.dispatcher.Object); Original = instanceMessage; }
MockController(TController controller, Mock <IDispatcher> dispatcher) { this.dispatcher = dispatcher; this.originalController = controller; this.httpContext = Pleasure.Mock <HttpContextBase>(); var routeData = new RouteData(); routeData.Values.Add("Action", "Action"); routeData.Values.Add("Controller", "Controller"); this.originalController.ControllerContext = new ControllerContext(this.httpContext.Object, routeData, Pleasure.Mock <ControllerBase>().Object); var routes = new RouteCollection(); routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); this.originalController.Url = new UrlHelper(new RequestContext(this.httpContext.Object, routeData), routes); AddViewEngine(); }
public MockMessage <TMessage, TResult> StubEmptyQuery <TEntity>(OrderSpecification <TEntity> orderSpecification = null, Specification <TEntity> whereSpecification = null, FetchSpecification <TEntity> fetchSpecification = null, PaginatedSpecification paginatedSpecification = null) where TEntity : class, IEntity, new() { return(Stub(message => message.repository.StubQuery(orderSpecification, whereSpecification, fetchSpecification, paginatedSpecification, Pleasure.ToArray <TEntity>()))); }
object GenerateValueOrEmpty(Type propertyType, bool isEmpty) { object value = null; if (propertyType.IsEnum) { value = isEmpty ? 0 : Pleasure.Generator.EnumAsInt(propertyType); } else if (propertyType.IsAnyEquals(typeof(string), typeof(object))) { value = isEmpty ? string.Empty : Pleasure.Generator.String(); } else if (propertyType == typeof(bool) || propertyType == typeof(bool?)) { // ReSharper disable SimplifyConditionalTernaryExpression value = isEmpty ? false : Pleasure.Generator.Bool(); } // ReSharper restore SimplifyConditionalTernaryExpression else if (propertyType.IsAnyEquals(typeof(int), typeof(int?))) { value = isEmpty ? default(int) : Pleasure.Generator.PositiveNumber(1); } else if (propertyType.IsAnyEquals(typeof(long), typeof(long?))) { value = isEmpty ? default(long) : (long)Pleasure.Generator.PositiveNumber(1); } else if (propertyType.IsAnyEquals(typeof(float), typeof(float?))) { value = isEmpty ? default(float) : Pleasure.Generator.PositiveFloating(); } else if (propertyType.IsAnyEquals(typeof(decimal), typeof(decimal?))) { value = isEmpty ? default(decimal) : Pleasure.Generator.PositiveDecimal(); } else if (propertyType.IsAnyEquals(typeof(double), typeof(double?))) { value = isEmpty ? default(double) : (double)Pleasure.Generator.PositiveFloating(); } else if (propertyType == typeof(byte) || propertyType == typeof(byte?)) { value = isEmpty ? default(byte) : (byte)Pleasure.Generator.PositiveNumber(); } else if (propertyType == typeof(char) || propertyType == typeof(char?)) { value = isEmpty ? default(char) : Pleasure.Generator.String()[0]; } else if (propertyType == typeof(DateTime)) { value = isEmpty ? new DateTime() : Pleasure.Generator.DateTime(); } else if (propertyType == typeof(TimeSpan)) { value = isEmpty ? new TimeSpan() : Pleasure.Generator.TimeSpan(); } else if (propertyType.IsAnyEquals(typeof(Stream), typeof(MemoryStream))) { value = isEmpty ? Pleasure.Generator.Stream(0) : Pleasure.Generator.Stream(); } else if (propertyType == typeof(byte[])) { value = isEmpty ? Pleasure.ToArray <byte>() : Pleasure.Generator.Bytes(); } else if (propertyType == typeof(Guid) || propertyType == typeof(Guid?)) { value = isEmpty ? Guid.Empty : Guid.NewGuid(); } else if (propertyType == typeof(int[])) { value = isEmpty ? Pleasure.ToArray <int>() : Pleasure.ToArray(Pleasure.Generator.PositiveNumber(1)); } else if (propertyType == typeof(string[])) { value = isEmpty ? Pleasure.ToArray <string>() : Pleasure.ToArray(Pleasure.Generator.String()); } else if (propertyType.IsAnyEquals(typeof(HttpPostedFile), typeof(HttpPostedFileBase))) { value = isEmpty ? null : Pleasure.Generator.HttpPostedFile(); } else if (propertyType == typeof(Dictionary <string, string>)) { value = isEmpty ? new Dictionary <string, string>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() }); } else if (propertyType == typeof(Dictionary <string, object>)) { value = isEmpty ? new Dictionary <string, object>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() }).ToDictionary(r => r.Key, r => (object)r.Value); } return(value); }
public static void StubQuery <TEntity>(this Mock <IRepository> repository, OrderSpecification <TEntity> orderSpecification = null, Specification <TEntity> whereSpecification = null, FetchSpecification <TEntity> fetchSpecification = null, PaginatedSpecification paginatedSpecification = null, params TEntity[] entities) where TEntity : class, IEntity, new() { repository .Setup(r => r.Query(orderSpecification, Pleasure.MockIt.IsStrong(whereSpecification, dsl => dsl.IncludeAllFields()), fetchSpecification, Pleasure.MockIt.IsStrong(paginatedSpecification, dsl => dsl.IncludeAllFields()))) .Returns(Pleasure.ToQueryable(entities)); }
object GenerateValueOrEmpty(Type propertyType, bool isEmpty) { object value = null; bool isNullable = propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>); propertyType = isNullable ? propertyType.GetGenericArguments()[0] : propertyType; if (propertyType.IsEnum) { value = isEmpty ? 0 : Enum.Parse(propertyType, Pleasure.Generator.EnumAsInt(propertyType).ToString(), true); } else if (propertyType.IsAnyEquals(typeof(string), typeof(object))) { value = isEmpty ? string.Empty : Pleasure.Generator.String(); } else if (propertyType == typeof(bool) || propertyType == typeof(bool?)) { // ReSharper disable SimplifyConditionalTernaryExpression value = isEmpty ? false : Pleasure.Generator.Bool(); } // ReSharper restore SimplifyConditionalTernaryExpression else if (propertyType.IsAnyEquals(typeof(int))) { value = isEmpty ? default(int) : Pleasure.Generator.PositiveNumber(1); } else if (propertyType.IsAnyEquals(typeof(long))) { value = isEmpty ? default(long) : (long)Pleasure.Generator.PositiveNumber(1); } else if (propertyType.IsAnyEquals(typeof(short))) { value = isEmpty ? default(short) : (short)Pleasure.Generator.PositiveNumber(1); } else if (propertyType.IsAnyEquals(typeof(float))) { value = isEmpty ? default(float) : Pleasure.Generator.PositiveFloating(); } else if (propertyType.IsAnyEquals(typeof(decimal))) { value = isEmpty ? default(decimal) : Pleasure.Generator.PositiveDecimal(); } else if (propertyType.IsAnyEquals(typeof(double))) { value = isEmpty ? default(double) : Pleasure.Generator.PositiveDouble(); } else if (propertyType.IsAnyEquals(typeof(byte), typeof(sbyte))) { value = isEmpty ? default(byte) : (byte)Pleasure.Generator.PositiveNumber(); } else if (propertyType == typeof(char)) { value = isEmpty ? default(char) : Pleasure.Generator.String()[0]; } else if (propertyType.IsAnyEquals(typeof(DateTime))) { value = isEmpty ? new DateTime() : Pleasure.Generator.DateTime(); } else if (propertyType.IsAnyEquals(typeof(TimeSpan))) { value = isEmpty ? new TimeSpan() : Pleasure.Generator.TimeSpan(); } else if (propertyType.IsAnyEquals(typeof(Stream), typeof(MemoryStream))) { value = isEmpty ? Pleasure.Generator.Stream(0) : Pleasure.Generator.Stream(); } else if (propertyType == typeof(byte[])) { value = isEmpty ? Pleasure.ToArray <byte>() : Pleasure.Generator.Bytes(); } else if (propertyType == typeof(Guid)) { value = isEmpty ? Guid.Empty : Guid.NewGuid(); } else if (propertyType == typeof(int[])) { value = isEmpty ? Pleasure.ToArray <int>() : Pleasure.ToArray(Pleasure.Generator.PositiveNumber(1)); } else if (propertyType == typeof(string[])) { value = isEmpty ? Pleasure.ToArray <string>() : Pleasure.ToArray(Pleasure.Generator.String()); } else if (propertyType.IsAnyEquals(typeof(HttpPostedFile), typeof(HttpPostedFileBase))) { value = isEmpty ? null : Pleasure.Generator.HttpPostedFile(); } else if (propertyType == typeof(Dictionary <string, string>)) { value = isEmpty ? new Dictionary <string, string>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() }); } else if (propertyType == typeof(Dictionary <string, object>)) { value = isEmpty ? new Dictionary <string, object>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() }).ToDictionary(r => r.Key, r => (object)r.Value); } else if (propertyType == typeof(SqlConnection)) { value = new SqlConnection(@"Data Source={0};Database={1};Integrated Security=true;".F(Pleasure.Generator.String(length: 5), Pleasure.Generator.String(length: 5))); } return(isNullable ? Activator.CreateInstance(typeof(Nullable <>).MakeGenericType(propertyType), value) : value); }