コード例 #1
0
        public void WithViewContextWithoutValidationSummaryMessageElementShouldSetItToAccessor()
        {
            var mvcOptions = new MvcViewOptions()
            {
                HtmlHelperOptions = new HtmlHelperOptions()
            };

            mvcOptions.HtmlHelperOptions.ValidationSummaryMessageElement = "Test";

            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddSingleton(mvcOptions);
                services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
            });

            MyViewComponent <AccessorComponent>
            .Instance()
            .WithViewContext(viewContext =>
            {
                viewContext.ValidationSummaryMessageElement = null;
            })
            .ShouldPassForThe <AccessorComponent>(viewComponent =>
            {
                Assert.NotNull(viewComponent.ViewContext.ValidationSummaryMessageElement);
                Assert.True(viewComponent.ViewContext.ValidationSummaryMessageElement == "Test");
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #2
0
        public void IActionContextAccessorShouldWorkCorrectlySynchronously()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
            });

            ActionContext firstContext  = null;
            ActionContext secondContext = null;

            MyViewComponent <AccessorComponent>
            .Instance()
            .ShouldPassForThe <AccessorComponent>(viewComponent =>
            {
                firstContext = viewComponent.ActionContext;
            });

            MyViewComponent <AccessorComponent>
            .Instance()
            .ShouldPassForThe <AccessorComponent>(viewComponent =>
            {
                secondContext = viewComponent.ActionContext;
            });

            Assert.NotNull(firstContext);
            Assert.NotNull(secondContext);
            Assert.IsAssignableFrom <ViewContextMock>(firstContext);
            Assert.IsAssignableFrom <ViewContextMock>(secondContext);
            Assert.NotSame(firstContext, secondContext);

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #3
0
        public void WithViewContextWithoutViewDataShouldSetItToAccessor()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
            });

            ViewDataDictionary viewData = null;

            MyViewComponent <AccessorComponent>
            .Instance()
            .WithViewContext(viewContext =>
            {
                viewContext.ViewData = null;
            })
            .ShouldPassForThe <AccessorComponent>(viewComponent =>
            {
                viewData = viewComponent.ViewContext.ViewData;
            });

            Assert.NotNull(viewData);
            Assert.IsAssignableFrom <ViewDataDictionaryMock>(viewData);

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #4
0
        public void WithViewComponentContextWithoutViewComponentDescriptorShouldSetItToAccessor()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
            });

            var context = new ViewComponentContext {
                ViewComponentDescriptor = null
            };

            MyViewComponent <AccessorComponent>
            .Instance()
            .WithViewComponentContext(context)
            .ShouldPassForThe <AccessorComponent>(viewComponent =>
            {
                Assert.NotNull(viewComponent);
                Assert.NotNull(viewComponent.ViewComponentContext);
                Assert.NotNull(viewComponent.ViewComponentContext.ViewComponentDescriptor);
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #5
0
 public void ForumSideBarViewComponentShouldReturnViewWithOut()
 => MyViewComponent <RandomJobsViewComponent>
 .Instance()
 .WithData(GetJob())
 .InvokedWith(v => v.Invoke())
 .ShouldReturn()
 .View(v => v.WithModelOfType <RandomJobsModel>());
コード例 #6
0
 public void HttpResponsePredicateShouldWorkCorrectly()
 {
     MyViewComponent <NormalComponent>
     .Instance()
     .InvokedWith(c => c.Invoke())
     .ShouldPassForThe <HttpResponse>(response => response != null);
 }
コード例 #7
0
        public void ContainingEntryWithKeyShouldNotThrowExceptionWithCorrectViewComponentEntry()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <AddSessionComponent>
            .Instance()
            .InvokedWith(c => c.Invoke())
            .ShouldHave()
            .Session(session => session
                     .ContainingEntryWithKey("Integer")
                     .AndAlso()
                     .ContainingEntryWithKey("String"))
            .AndAlso()
            .ShouldReturn()
            .View();

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #8
0
        public void ContainingEntryWithKeyShouldThrowExceptionWithIncorrectViewComponentEntry()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            Test.AssertException <DataProviderAssertionException>(
                () =>
            {
                MyViewComponent <AddSessionComponent>
                .Instance()
                .InvokedWith(c => c.Invoke())
                .ShouldHave()
                .Session(session => session.ContainingEntryWithKey("Invalid"))
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                "When invoking AddSessionComponent expected session to have entry with 'Invalid' key, but such was not found.");

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #9
0
 public void ForumSideBarViewComponentShouldReturnView(int adCount)
 => MyViewComponent <RandomAdsViewComponent>
 .Instance()
 .WithData(GetAd())
 .InvokedWith(v => v.Invoke(adCount))
 .ShouldReturn()
 .View(v => v.WithModelOfType <RandomAdsModel>());
コード例 #10
0
        public void SessionWithNoNumberShouldThrowExceptionWithNoEntries()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            Test.AssertException <DataProviderAssertionException>(
                () =>
            {
                MyViewComponent <NormalComponent>
                .Instance()
                .InvokedWith(c => c.Invoke())
                .ShouldHave()
                .Session()
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                "When invoking NormalComponent expected to have session entries, but none were found.");

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #11
0
        public void NoSessionShouldThrowExceptionWithEntries()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            Test.AssertException <DataProviderAssertionException>(
                () =>
            {
                MyViewComponent <AddSessionComponent>
                .Instance()
                .InvokedWith(c => c.Invoke())
                .ShouldHave()
                .NoSession()
                .AndAlso()
                .ShouldReturn()
                .View();
            },
                "When invoking AddSessionComponent expected to have session with no entries, but in fact it had some.");

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #12
0
        public void WithViewContextFuncShouldSetItToAccessor()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
            });

            var actionDescriptor = new ActionDescriptor {
                DisplayName = "Test"
            };

            MyViewComponent <AccessorComponent>
            .Instance()
            .WithViewContext(context =>
            {
                context.ActionDescriptor = actionDescriptor;
            })
            .ShouldPassForThe <AccessorComponent>(viewComponent =>
            {
                Assert.NotNull(viewComponent);
                Assert.NotNull(viewComponent.ActionContext);
                Assert.Equal("Test", viewComponent.ActionContext.ActionDescriptor.DisplayName);
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #13
0
        public void DbContextShouldNotThrowExceptionWithCorrectAssertions()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddDbContext <CustomDbContext>(options => options.UseInMemoryDatabase());
            });

            MyViewComponent <CreateDataComponent>
            .Instance()
            .InvokedWith(c => c.Invoke(new CustomModel {
                Id = 1, Name = "Test"
            }))
            .ShouldHave()
            .DbContext(dbContext => dbContext
                       .WithEntities <CustomDbContext>(db =>
            {
                Assert.NotNull(db.Models.FirstOrDefaultAsync(m => m.Id == 1));
            }))
            .AndAlso()
            .ShouldReturn()
            .View();

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #14
0
        public void WithAuthenticatedUserShouldPopulateIdAndUserNameAndRoleProperly()
        {
            MyViewComponent <UserComponent>
            .Instance()
            .WithUser("IdentityIdentifier", "NewUserName", new List <string>
            {
                "Administrator"
            })
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .View()
            .ShouldPassForThe <UserComponent>(viewComponent =>
            {
                var user          = viewComponent.User as ClaimsPrincipal;
                var usernameClaim = viewComponent.User.Identity;

                Assert.NotNull(user);
                Assert.True(user.IsInRole("Administrator"));
                Assert.True(user.HasClaim(ClaimTypes.NameIdentifier, "IdentityIdentifier"));
                Assert.Equal("NewUserName", user.Identity.Name);
                Assert.True(user.HasClaim(ClaimTypes.Name, "NewUserName"));
                Assert.Equal("Passport", user.Identity.AuthenticationType);
                Assert.True(user.Identity.IsAuthenticated);
            });
        }
コード例 #15
0
 public void NoAttributesShouldNotThrowExceptionWithControllerContainingNoAttributes()
 {
     MyViewComponent <NormalComponent>
     .Instance()
     .ShouldHave()
     .NoAttributes();
 }
コード例 #16
0
 public void AttributesShouldNotThrowEceptionWithControllerContainingNumberOfAttributes()
 {
     MyViewComponent <AttributesComponent>
     .Instance()
     .ShouldHave()
     .Attributes(withTotalNumberOf: 2);
 }
コード例 #17
0
 public void ThatEqualsShouldNotThrowExceptionWhenProvidedMessageIsValidInViewComponent()
 {
     MyViewComponent <NormalComponent>
     .Instance()
     .WithSetup(vc =>
     {
         vc.ModelState.AddModelError("RequiredString", "The RequiredString field is required.");
         vc.ModelState.AddModelError("Integer", $"The field Integer must be between {1} and {int.MaxValue}.");
     })
     .InvokedWith(c => c.Invoke())
     .ShouldHave()
     .ModelState(modelState => modelState.For <RequestModel>()
                 .ContainingNoErrorFor(m => m.NonRequiredString)
                 .ContainingErrorFor(m => m.RequiredString).ThatEquals("The RequiredString field is required.")
                 .AndAlso()
                 .ContainingErrorFor(m => m.RequiredString)
                 .AndAlso()
                 .ContainingNoErrorFor(m => m.NotValidateInteger)
                 .AndAlso()
                 .ContainingError("RequiredString")
                 .ContainingErrorFor(m => m.Integer).ThatEquals($"The field Integer must be between {1} and {int.MaxValue}.")
                 .ContainingError("RequiredString")
                 .ContainingError("Integer")
                 .ContainingNoErrorFor(m => m.NotValidateInteger));
 }
コード例 #18
0
        public void WithEntitesShouldSetupDbContext()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddDbContext <CustomDbContext>(options =>
                                                        options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));
            });

            MyViewComponent <FindDataComponent>
            .Instance()
            .WithDbContext(dbContext => dbContext
                           .WithEntities <CustomDbContext>(db => db
                                                           .Models.Add(new CustomModel
            {
                Id   = 1,
                Name = "Test"
            })))
            .InvokedWith(c => c.Invoke(1))
            .ShouldReturn()
            .View()
            .WithModelOfType <CustomModel>()
            .Passing(m => m.Name == "Test");

            MyViewComponent <FindDataComponent>
            .Instance()
            .WithDbContext(dbContext => dbContext
                           .WithEntities(db => db.Add(new CustomModel
            {
                Id   = 1,
                Name = "Test"
            })))
            .InvokedWith(c => c.Invoke(1))
            .ShouldReturn()
            .View()
            .WithModelOfType <CustomModel>()
            .Passing(m => m.Name == "Test");

            MyViewComponent <FindDataComponent>
            .Instance()
            .WithDbContext(dbContext => dbContext
                           .WithEntities <CustomDbContext>(db => db
                                                           .Models.Add(new CustomModel
            {
                Id   = 2,
                Name = "Test"
            })))
            .InvokedWith(c => c.Invoke(1))
            .ShouldReturn()
            .Content("Invalid");

            MyViewComponent <FindDataComponent>
            .Instance()
            .InvokedWith(c => c.Invoke(1))
            .ShouldReturn()
            .Content("Invalid");

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #19
0
        public void WithEntriesAsByteDictionaryAndWrongKeyShouldNotSetCorrectEntryInViewComponent()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <FullSessionComponent>
            .Instance()
            .WithSession(session => session
                         .WithEntries(new Dictionary <string, byte[]>
            {
                ["InvalidEntry"] = new byte[] { 1, 2, 3 },
                ["Test"]         = null
            }))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .Content("Invalid");

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #20
0
        public void WithEntriesAsIntDictionaryShouldSetCorrectEntryInViewComponent()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <FullSessionComponent>
            .Instance()
            .WithSession(session => session
                         .WithEntries(new Dictionary <string, int>
            {
                ["IntEntry"] = 1
            }))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .View()
            .WithModel(1);

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #21
0
        public void WithWrongEntryKeyAndEntriesAsObjectShouldNotSetEntryInViewComponent()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <FullSessionComponent>
            .Instance()
            .WithSession(session => session
                         .WithEntries(new
            {
                InvalidStringKey = "value",
                InvalidIntKey    = 1,
                InvalidByteKey   = new byte[] { 1, 2, 3 }
            }))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .Content("Invalid");

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #22
0
        public void WithEntriesAsObjectShouldSetCorrectEntryInViewComponent()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddMemoryCache();
                services.AddDistributedMemoryCache();
                services.AddSession();
            });

            MyViewComponent <FullSessionComponent>
            .Instance()
            .WithSession(session => session
                         .WithEntries(new
            {
                StringEntry = "test",
                IntEntry    = 1,
                ByteEntry   = new byte[] { 1, 2, 3 }
            }))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .View()
            .WithModel(new byte[] { 1, 2, 3 });

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #23
0
        public void WithAuthenticatedUserShouldPopulateProperUserWhenUserWithUserBuilder()
        {
            MyViewComponent <UserComponent>
            .Instance()
            .WithUser(user => user
                      .WithUsername("NewUserName")
                      .WithAuthenticationType("Custom")
                      .InRole("NormalUser")
                      .AndAlso()
                      .InRoles("Moderator", "Administrator")
                      .InRoles(new[]
            {
                "SuperUser",
                "MegaUser"
            }))
            .InvokedWith(c => c.Invoke())
            .ShouldReturn()
            .View()
            .ShouldPassForThe <UserComponent>(viewComponent =>
            {
                var user = viewComponent.User as ClaimsPrincipal;

                Assert.Equal("NewUserName", user.Identity.Name);
                Assert.Equal("Custom", user.Identity.AuthenticationType);
                Assert.True(user.Identity.IsAuthenticated);
                Assert.True(user.IsInRole("NormalUser"));
                Assert.True(user.IsInRole("Moderator"));
                Assert.True(user.IsInRole("Administrator"));
                Assert.True(user.IsInRole("SuperUser"));
                Assert.True(user.IsInRole("MegaUser"));
                Assert.False(user.IsInRole("AnotherRole"));
            });
        }
 public void ContainingAttributeOfTypeShouldNotThrowExceptionWithViewComponentWithTheAttribute()
 {
     MyViewComponent <AttributesComponent>
     .Instance()
     .InvokedWith(c => c.Invoke())
     .ShouldHave()
     .Attributes(attributes => attributes.ContainingAttributeOfType <CustomAttribute>());
 }
 public void AttributesShouldNotThrowEceptionWithActionContainingNumberOfAttributes()
 {
     MyViewComponent <AttributesComponent>
     .Instance()
     .InvokedWith(c => c.Invoke())
     .ShouldHave()
     .Attributes(withTotalNumberOf: 2);
 }
 public void NoAttributesShouldNotThrowExceptionWithActionContainingNoAttributes()
 {
     MyViewComponent <NormalComponent>
     .Instance()
     .InvokedWith(c => c.Invoke())
     .ShouldHave()
     .NoAttributes();
 }
 public void PassingForShouldNotThrowExceptionWithCorrectPredicate()
 {
     MyViewComponent <AttributesComponent>
     .Instance()
     .ShouldHave()
     .Attributes(attributes => attributes
                 .PassingFor <ViewComponentAttribute>(route => route.Name == "Test"));
 }
コード例 #28
0
 public void ShouldReturnViewShouldNotThrowExceptionWithDefaultView()
 {
     MyViewComponent <ViewResultComponent>
     .Instance()
     .InvokedWith(c => c.Invoke(null))
     .ShouldReturn()
     .View();
 }
コード例 #29
0
 public void ShouldReturnViewWithNameShouldNotThrowExceptionWithCorrectName()
 {
     MyViewComponent <ViewResultComponent>
     .Instance()
     .InvokedWith(c => c.Invoke("custom"))
     .ShouldReturn()
     .View("Custom");
 }
 public void PassingForShouldNotThrowExceptionWithCorrectAssertions()
 {
     MyViewComponent <AttributesComponent>
     .Instance()
     .ShouldHave()
     .Attributes(attributes => attributes
                 .PassingFor <ViewComponentAttribute>(vc => Assert.Equal("Test", vc.Name)));
 }