Esempio n. 1
0
        public void DefaultConfigAndAdditionalServicesShouldWorkCorrectly()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddMvc();
            });

            var service = TestServiceProvider.GetRequiredService <MvcMarkerService>();

            Assert.NotNull(service);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void DefaultConfigAndAdditionalRoutesShouldSetOnlyThem()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithRoutes(routes =>
            {
                routes.MapRoute(
                    name: "another",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            var setRoutes = TestApplication.Router as RouteCollection;

            Assert.NotNull(setRoutes);
            Assert.Equal(3, setRoutes.Count);
        }
        public void ToDataTokenShouldThrowExceptionWithIncorrectDataTokenKey()
        {
            MyMvc.StartsFrom <RoutesStartup>();

            Test.AssertException <RouteAssertionException>(
                () =>
            {
                MyMvc
                .Routes()
                .ShouldMap("/Test")
                .ToDataToken("name");
            },
                "Expected route '/Test' to contain data token with 'name' key but such was not found.");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void ExplicitMockedMemoryCacheShouldOverrideIt()
        {
            MyMvc
            .StartsFrom <DataStartup>()
            .WithServices(services =>
            {
                services.ReplaceMemoryCache();
            });

            var memoryCache = TestServiceProvider.GetService <IMemoryCache>();

            Assert.NotNull(memoryCache);
            Assert.IsAssignableFrom <MockedMemoryCache>(memoryCache);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void ToDataTokenShouldThrowExceptionWithIncorrectDataTokensWithSingleCountError()
        {
            MyMvc.StartsFrom <RoutesStartup>();

            Test.AssertException <RouteAssertionException>(
                () =>
            {
                MyMvc
                .Routes()
                .ShouldMap("/Test")
                .ToDataTokens(new { id = 1 });
            },
                "Expected route '/Test' to contain 1 data token but in fact found 2.");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void ToDataTokensShouldThrowExceptionWithIncorrectDataTokens()
        {
            MyMvc.StartsFrom <RoutesStartup>();

            Test.AssertException <RouteAssertionException>(
                () =>
            {
                MyMvc
                .Routes()
                .ShouldMap("/Test")
                .ToDataTokens(new { random = "value", another = "invalid" });
            },
                "Expected route '/Test' to contain data token with 'another' key and the provided value but the value was different.");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void WithResolvedDependencyForShouldChooseCorrectConstructorWithLessDependenciesForPocoController()
        {
            MyMvc
            .Controller <FullPocoController>()
            .WithServiceFor <IInjectedService>(new InjectedService())
            .ShouldPassFor()
            .TheController(controller =>
            {
                Assert.NotNull(controller);
                Assert.NotNull(controller.InjectedService);
                Assert.Null(controller.AnotherInjectedService);
                Assert.Null(controller.InjectedRequestModel);
            });

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void ExplicitMockedSessionShouldOverrideIt()
        {
            MyMvc
            .StartsFrom <SessionDataStartup>()
            .WithServices(services =>
            {
                services.ReplaceSession();
            });

            var session = TestServiceProvider.GetService <ISessionStore>();

            Assert.NotNull(session);
            Assert.IsAssignableFrom <MockedSessionStore>(session);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void ToDataTokensShouldThrowExceptionWithIncorrectDataTokensWithMultipleCountError()
        {
            MyMvc.StartsFrom <RoutesStartup>();

            Test.AssertException <RouteAssertionException>(
                () =>
            {
                MyMvc
                .Routes()
                .ShouldMap("/Test")
                .ToDataTokens(new { id = 1, query = "invalid", another = "another", fourth = "test" });
            },
                "Expected route '/Test' to contain 4 data tokens but in fact found 2.");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void ExplicitMockedTempDataProviderShouldOverrideIt()
        {
            MyMvc
            .StartsFrom <DataStartup>()
            .WithServices(services =>
            {
                services.ReplaceTempDataProvider();
            });

            var tempDataProvider = TestServiceProvider.GetService <ITempDataProvider>();

            Assert.NotNull(tempDataProvider);
            Assert.IsAssignableFrom <MockedTempDataProvider>(tempDataProvider);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void IsUsingWithAdditionalServicesShouldUseThem()
        {
            MyMvc
            .StartsFrom <CustomStartup>()
            .WithServices(services =>
            {
                services.AddTransient <IInjectedService, InjectedService>();
            });

            var injectedServices = TestServiceProvider.GetService <IInjectedService>();

            Assert.NotNull(injectedServices);
            Assert.IsAssignableFrom(typeof(InjectedService), injectedServices);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void WithRequestShouldNotWorkWithDefaultRequestActionForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            MyMvc
            .Controller <FullPocoController>()
            .Calling(c => c.WithRequest())
            .ShouldReturn()
            .BadRequest();

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void CallingShouldPopulateCorrectActionNameWithTaskActionCallForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            var voidActionResultTestBuilder = MyMvc
                                              .Controller <FullPocoController>()
                                              .Calling(c => c.EmptyActionAsync());

            this.CheckActionName(voidActionResultTestBuilder, "EmptyActionAsync");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void CallingShouldPopulateCorrectActionNameAndActionResultWithNormalActionCallForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            var actionResultTestBuilder = MyMvc
                                          .Controller <FullPocoController>()
                                          .Calling(c => c.OkResultAction());

            this.CheckActionResultTestBuilder(actionResultTestBuilder, "OkResultAction");

            MyMvc.IsUsingDefaultConfiguration();
        }
Esempio n. 15
0
        public void CallingShouldWorkCorrectlyWithFromServices()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.WithService(From.Services <IHttpContextAccessor>()))
            .ShouldReturn()
            .Ok();

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void MemoryCacheWithNumberShouldNotThrowExceptionWithCorrectCacheEntries()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services => services.AddMemoryCache());

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.AddMemoryCacheAction())
            .ShouldHave()
            .MemoryCache(withNumberOfEntries: 2)
            .AndAlso()
            .ShouldReturn()
            .Ok();

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void NoMemoryCacheShouldNotThrowExceptionWithNoCacheEntries()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services => services.AddMemoryCache());

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.Ok())
            .ShouldHave()
            .NoMemoryCache()
            .AndAlso()
            .ShouldReturn()
            .Ok();

            MyMvc.IsUsingDefaultConfiguration();
        }
Esempio n. 18
0
        public void WithSetShouldSetupDbContext()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddDbContext <CustomDbContext>(options =>
                                                        options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));
            });

            MyMvc
            .Controller <DbContextController>()
            .WithDbContext(dbContext => dbContext
                           .WithSet <CustomDbContext, CustomModel>(set => set
                                                                   .Add(new CustomModel
            {
                Id   = 1,
                Name = "Test"
            })))
            .Calling(c => c.Find(1))
            .ShouldReturn()
            .Ok()
            .WithResponseModelOfType <CustomModel>()
            .Passing(m => m.Name == "Test");

            MyMvc
            .Controller <DbContextController>()
            .WithDbContext(dbContext => dbContext
                           .WithSet <CustomDbContext, CustomModel>(set => set
                                                                   .Add(new CustomModel
            {
                Id   = 2,
                Name = "Test"
            })))
            .Calling(c => c.Find(1))
            .ShouldReturn()
            .NotFound();

            MyMvc
            .Controller <DbContextController>()
            .Calling(c => c.Find(1))
            .ShouldReturn()
            .NotFound();

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void ControllerWithNoParameterlessConstructorAndNoServicesShouldThrowProperException()
        {
            MyMvc.IsUsingDefaultConfiguration();

            Test.AssertException <UnresolvedServicesException>(
                () =>
            {
                MyMvc
                .Controller <NoParameterlessConstructorController>()
                .Calling(c => c.OkAction())
                .ShouldReturn()
                .Ok();
            },
                "NoParameterlessConstructorController could not be instantiated because it contains no constructor taking no parameters.");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void WithRequestShouldWorkWithSetRequestActionForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            MyMvc
            .Controller <FullPocoController>()
            .WithHttpRequest(req => req.WithFormField("Test", "TestValue"))
            .Calling(c => c.WithRequest())
            .ShouldReturn()
            .Ok();

            MyMvc.IsUsingDefaultConfiguration();
        }
Esempio n. 21
0
        public void AtShouldWorkCorrectlyWithCorrectTaskActionCall()
        {
            MyMvc.StartsFrom <RoutesStartup>();

            Test.AssertException <RedirectResultAssertionException>(
                () =>
            {
                MyMvc
                .Controller <MvcController>()
                .Calling(c => c.RedirectToRouteAction())
                .ShouldReturn()
                .Redirect()
                .To <MvcController>(c => c.AsyncOkResultAction());
            },
                "When calling RedirectToRouteAction action in MvcController expected redirect result to have resolved location to '/api/test', but in fact received '/api/Redirect/WithParameter?id=1'.");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void MemoryCacheWithBuilderShouldWorkCorrectly()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services => services.AddMemoryCache());

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.AddMemoryCacheAction())
            .ShouldHave()
            .MemoryCache(cache => cache
                         .ContainingEntry("test", "value"))
            .AndAlso()
            .ShouldReturn()
            .Ok();

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void RouteDataShouldBePopulatedWhenRequestAndPathAreProvidedForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            MyMvc
            .Controller <FullPocoController>()
            .WithHttpRequest(req => req.WithPath("/Mvc/WithRouteData/1"))
            .Calling(c => c.WithRouteData(1))
            .ShouldReturn()
            .View();

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void WithoutValidationShouldNotValidateTheRequestModelForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            MyMvc
            .Controller <FullPocoController>()
            .WithoutValidation()
            .Calling(c => c.ModelStateCheck(TestObjectFactory.GetRequestModelWithErrors()))
            .ShouldHave()
            .ValidModelState();

            MyMvc.IsUsingDefaultConfiguration();
        }
Esempio n. 25
0
        public void ToShouldWorkCorrectly()
        {
            MyMvc.StartsFrom <RoutesStartup>();

            Test.AssertException <RedirectResultAssertionException>(
                () =>
            {
                MyMvc
                .Controller <MvcController>()
                .Calling(c => c.LocalRedirectActionWithCustomUrlHelper(null))
                .ShouldReturn()
                .LocalRedirect()
                .To <NoAttributesController>(c => c.WithParameter(1));
            },
                "When calling LocalRedirectActionWithCustomUrlHelper action in MvcController expected local redirect result to have resolved location to '/api/Redirect/WithParameter?id=1', but in fact received '/api/test'.");

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void DefaultConfigurationWithSessionShouldSetMockedSession()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            var session = TestServiceProvider.GetService <ISessionStore>();

            Assert.NotNull(session);
            Assert.IsAssignableFrom <MockedSessionStore>(session);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void WithHttpContextFactoryShouldReturnMockedHttpContextBasedOnTheFactoryCreatedHttpContext()
        {
            MyMvc.IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.ReplaceTransient <IHttpContextFactory, CustomHttpContextFactory>();
            });

            var httpContextFactory = TestServiceProvider.GetService <IHttpContextFactory>();

            Assert.NotNull(httpContextFactory);

            var httpContext = TestHelper.CreateMockedHttpContext();

            Assert.NotNull(httpContext);
            Assert.Equal(ContentType.AudioVorbis, httpContext.Request.ContentType);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void WithoutHttpContextFactoryTheDefaultMockedHttpContextShouldBeProvided()
        {
            MyMvc.IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.RemoveTransient <IHttpContextFactory>();
            });

            var httpContextFactory = TestServiceProvider.GetService <IHttpContextFactory>();

            Assert.Null(httpContextFactory);

            var httpContext = TestHelper.CreateMockedHttpContext();

            Assert.NotNull(httpContext);
            Assert.Equal(ContentType.FormUrlEncoded, httpContext.Request.ContentType);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void CustomStartupWithAdditionalServiceShouldSetThem()
        {
            MyMvc.StartsFrom <CustomStartup>()
            .WithServices(services =>
            {
                services.AddTransient <IAnotherInjectedService, AnotherInjectedService>();
            });

            var service = TestApplication.Services.GetRequiredService <IAnotherInjectedService>();

            Assert.NotNull(service);

            var routes = TestApplication.Router as RouteCollection;

            Assert.NotNull(routes);
            Assert.Equal(2, routes.Count);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public void UsingUrlHelperInsideControllerShouldWorkCorrectlyForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            MyMvc
            .Controller <FullPocoController>()
            .WithRouteData()
            .Calling(c => c.UrlAction())
            .ShouldReturn()
            .Ok()
            .WithResponseModel("/FullPoco/UrlAction");

            MyMvc.IsUsingDefaultConfiguration();
        }