public void Can_Generate_User_Sequance() { //Arrange var mockUserManager = new Mock <FakeUserManager>(); mockUserManager.Setup(m => m.Users).Returns(new AppUser[] { //new AppUser { Id = "1", UserName="******", RoleId="A" }, //new AppUser { Id = "2", UserName="******", RoleId="A" }, //new AppUser { Id = "3", UserName="******", RoleId="B" }, //new AppUser { Id = "4", UserName="******", RoleId="A" }, //new AppUser { Id = "5", UserName="******", RoleId="C" }, }.AsQueryable); var mockRoleManager = new Mock <FakeRoleManager>(); mockRoleManager.Setup(m => m.Roles).Returns(new AppRole[] { new AppRole { Id = "A", Name = "Admin" }, new AppRole { Id = "B", Name = "Manager" }, new AppRole { Id = "C", Name = "Employee" }, }.AsQueryable); mockRoleManager.Setup(m => m.FindByIdAsync(It.Is <string>(v => v == "A"))) .ReturnsAsync(mockRoleManager.Object.Roles.Where(r => r.Id == "A").FirstOrDefault()); RoleUsersTagHelper target = new RoleUsersTagHelper(mockUserManager.Object, mockRoleManager.Object) { RoleId = "A" }; TagHelperContext tagContext = new TagHelperContext(new TagHelperAttributeList(), new Dictionary <object, object>(), ""); TagHelperOutput output = new TagHelperOutput("", new TagHelperAttributeList(), (cach, encoder) => Task.FromResult(new Mock <TagHelperContent>().Object)); //Act target.ProcessAsync(tagContext, output); //Arrange Assert.Equal("Joe, Alice, Bob", output.Content.GetContent()); }
public async Task ProcessAsync_ReturnsContent() { // Setup var usrMgr = TestHelper.GetUserManager(); var roleMgr = TestHelper.GetRoleManager(); var user1 = new AppUser { Id = Guid.NewGuid().ToString(), UserName = "******" }; var user2 = new AppUser { Id = Guid.NewGuid().ToString(), UserName = "******" }; var users = new List <AppUser> { user1, user2 }; var identityRole = new IdentityRole { Name = "Members" }; usrMgr.Users.Returns(users.AsQueryable()); roleMgr.FindByIdAsync(identityRole.Name).Returns(identityRole); usrMgr.IsInRoleAsync(user1, identityRole.Name).Returns(true); usrMgr.IsInRoleAsync(user2, identityRole.Name).Returns(true); var tagHelper = new RoleUsersTagHelper(usrMgr, roleMgr) { Role = identityRole.Name }; TagHelperContext ctx = new TagHelperContext(new TagHelperAttributeList(), new Dictionary <object, object>(), ""); var content = Substitute.For <TagHelperContent>(); TagHelperOutput output = new TagHelperOutput("div", new TagHelperAttributeList(), (cache, encoder) => Task.FromResult(content)); // Act await tagHelper.ProcessAsync(ctx, output); // Assert var res = output.Content.GetContent(); Assert.Equal($"{user1.UserName}, {user2.UserName}", res); }