public void Index_AuthenticatedUser_ShouldDisplayTaskList() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Story>()); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Group>()); // Arrange var userStories = fixture.CreateMany <Story>(3).ToList(); var taskRepository = fixture.Freeze <Mock <IStoryRepository> >(); taskRepository.Setup(c => c.GetUserStories(It.IsAny <string>())).Returns(userStories); var userService = fixture.Freeze <Mock <IUserService> >(); userService.Setup(i => i.IsAuthenticated).Returns(true); userService.Setup(i => i.GetUserId()).Returns(userStories.First().UserId); var controller = fixture.CreateController <HomeController>(); // Act var result = controller.Index() as ViewResult; // Assert Assert.NotNull(result); Assert.Equal("StoryList", result.ViewName); Assert.IsType <List <Story> >(result.Model); Assert.Equal(userStories.Count, (result.Model as List <Story>).Count); }
public async Task Dasboard_ShouldQueryRepositories() { var fixture = FixtureExtensions.CreateFixture(); AddFixtureCustomizations(fixture); // Arrange var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.IsInRole(It.IsAny <StandardRoles>())).Returns(true); uService.Setup(s => s.IsInRole(It.IsAny <string>())).Returns(true); var upRepo = fixture.Freeze <Mock <IUserProfileRepository> >(); var interestRepo = fixture.Freeze <Mock <IInterestRepository> >(); var hashtagRepo = fixture.Freeze <Mock <IHashtagRepository> >(); var groupRepo = fixture.Freeze <Mock <IGroupRepository> >(); var sut = fixture.CreateController <AdminController>(); // Act var action = await sut.Dashboard(); // Assert upRepo.Verify(r => r.GetStrictInRoleUserProfilesAsync(It.Is <StandardRoles>(sr => sr == StandardRoles.Normal))); upRepo.Verify(r => r.GetUsersInRoleProfileAsync(It.Is <StandardRoles>(sr => sr == StandardRoles.Coach))); upRepo.Verify(r => r.GetStrictInRoleUserProfilesAsync(It.Is <StandardRoles>(sr => sr == StandardRoles.Admin))); interestRepo.Verify(r => r.AllAsync); hashtagRepo.Verify(r => r.AllAsync); groupRepo.Verify(r => r.AllAsync); }
public async Task Get_ShouldReturn_FilteredItems() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <UserProfileDomain.Models.UserProfile>()); // Arrange var testBirthDate = fixture.Create <DateTime>(); var nonMatchingCount = fixture.Create <int>(); var mathcingCount = fixture.Create <int>(); var matching = fixture.Build <UserProfileDomain.Models.UserProfile>() .With(p => p.BirthDate, testBirthDate) .CreateMany(mathcingCount); var profileStore = matching.Concat(fixture.CreateMany <UserProfileDomain.Models.UserProfile>(nonMatchingCount)).ToList(); var upContext = fixture.FreezeDbContext <IUserProfileContext>(); MockingHelpers.MockDbContextSet(upContext, c => c.Set <UserProfileDomain.Models.UserProfile>(), profileStore.AsQueryable()); var sut = fixture.Create <UserProfileRepository>(); // Act var actual = sut.Get(p => p.BirthDate == testBirthDate); var actualAsync = await sut.GetAsync(p => p.BirthDate == testBirthDate); //Assert Assert.Equal(matching, actual); Assert.Equal(matching, actualAsync); }
public void GroupMember_MapsTo_UserProfileRefVM() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <UserProfileDomain.Models.UserProfile>()); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Group>()); // Arrange var groupMembers = fixture.CreateMany <GroupMember>().ToList(); // Act var uprVM = Mapper.Map <IList <UserBasicInfoVM> >(groupMembers); // Assert Assert.Equal(groupMembers.Count, uprVM.Count); var joined = uprVM.Join(groupMembers, up => up.Id, gm => gm.UserProfile.Id, (up, gm) => new { up, gm }) .ToList(); Assert.Equal(groupMembers.Count, joined.Count); Assert.All(joined, j => { Assert.Equal(j.gm.UserProfile.FirstName, j.up.FirstName); Assert.Equal(j.gm.UserProfile.LastName, j.up.LastName); }); }
public async Task GetUserProfile_ShouldReturn_UserProfile() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <UserProfileDomain.Models.UserProfile>()); // Arrange var testUserId = fixture.Create <string>(); var count = fixture.Create <int>(); var expectedProfile = fixture.Build <UserProfileDomain.Models.UserProfile>() .With(p => p.UserId, testUserId) .Create(); var profileStore = new List <UserProfileDomain.Models.UserProfile>() { expectedProfile } .Concat(fixture.Build <UserProfileDomain.Models.UserProfile>() .With(p => p.UserId, fixture.Create <string>()) .CreateMany(count)); var upContext = fixture.FreezeDbContext <IUserProfileContext>(); MockingHelpers.MockDbContextSet(upContext, c => c.Set <UserProfileDomain.Models.UserProfile>(), profileStore.AsQueryable()); var sut = fixture.Create <UserProfileRepository>(); // Act var actual = sut.GetUserProfile(testUserId); var actualAsync = await sut.GetUserProfileAsync(testUserId); //Assert Assert.Equal(expectedProfile, actual); Assert.Equal(expectedProfile, actualAsync); }
public async Task GrantRole_ShouldReturnError_IfUserAlreadyInRole() { var fixture = FixtureExtensions.CreateFixture(); var grantRoleVM = fixture.Create <GrantRoleVM>(); var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.FindUserByIdAsync(It.IsAny <string>())) .Returns(Task.FromResult(new UserInfo())); uService.Setup(s => s.IsInRole(It.IsAny <string>(), It.IsAny <string>())) .Returns(true); var sut = fixture.CreateController <AdminController>(); // Act var action = await sut.GrantRole(grantRoleVM); var pView = action as PartialViewResult; // Assert uService.Verify(s => s.IsInRole(It.Is <string>(v => v == grantRoleVM.UserId), It.Is <string>(v => v == grantRoleVM.RoleName))); Assert.IsType <PartialViewResult>(action); Assert.Equal("_AjaxValidation", pView.ViewName); Assert.False(sut.ModelState.IsValid); Assert.Contains("UserAlreadyInRole", sut.ModelState.Keys); }
public void Constructor_ThrowsFor_NullUserManager() { var fixture = FixtureExtensions.CreateFixture(); // Arrange fixture.Register <ApplicationUserManager>(() => null); // Assert Assert.Throws <ArgumentNullException>("userManager", () => new UserService(null, null)); }
public void ShouldThrow_ArgumentNullExc_WithNull_StoryDbCtx() { var fixture = FixtureExtensions.CreateFixture(); // Arrange fixture.Register <IStoryContext>(() => null); // Assert Assert.Throws <ArgumentNullException>("context", () => new StoryRepository(null)); }
public void About() { var fixture = FixtureExtensions.CreateFixture(); // Arrange var controller = fixture.CreateController <HomeController>(); // Act ViewResult result = controller.About() as ViewResult; // Assert Assert.Equal("Your application description page.", result.ViewBag.Message); }
public void Contact() { var fixture = FixtureExtensions.CreateFixture(); // Arrange var controller = fixture.CreateController <HomeController>(); // Act ViewResult result = controller.Contact() as ViewResult; // Assert Assert.NotNull(result); }
public void Index_ShouldRedirectTo_Dashboard() { var fixture = FixtureExtensions.CreateFixture(); var sut = fixture.CreateController <AdminController>(); // Act var action = sut.Index(); var redirect = action as RedirectToRouteResult; // Assert Assert.IsType <RedirectToRouteResult>(action); Assert.Contains("action", redirect.RouteValues.Keys); Assert.Equal("Dashboard", redirect.RouteValues["action"]); }
public void Should_ReturnEmptyList_WithNullOrWhitespace_UserId(string userId) { var fixture = FixtureExtensions.CreateFixture(); // Arrange var storyContext = fixture.FreezeDbContext <IStoryContext>(); MockingHelpers.MockDbContextSet(storyContext, c => c.Stories, null); var sut = fixture.Create <StoryRepository>(); // Act var userStories = sut.GetUserStories(userId); // Assert Assert.Equal(0, userStories.Count); }
public async Task GrantRole_ShouldReturn_JsonResponse_OnSuccess() { var fixture = FixtureExtensions.CreateFixture(); var grantRoleVM = fixture.Create <GrantRoleVM>(); var urlLocation = fixture.Create <string>(); var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.FindUserByIdAsync(It.IsAny <string>())) .Returns(Task.FromResult(new UserInfo())); uService.Setup(s => s.IsInRole(It.IsAny <string>(), It.IsAny <string>())) .Returns(false); uService.Setup(s => s.AddToRoleAsync(It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(GrantRoleResult.Success)); var mockUrlHelper = new Mock <UrlHelper>(); mockUrlHelper.Setup(m => m.Action(It.IsAny <string>())) .Returns(urlLocation); var sut = fixture.CreateController <AdminController>(); sut.Url = mockUrlHelper.Object; // Act var action = await sut.GrantRole(grantRoleVM); var jsonResult = action as JsonResult; // Assert uService.Verify(s => s.IsInRole(It.Is <string>(v => v == grantRoleVM.UserId), It.Is <string>(v => v == grantRoleVM.RoleName))); Assert.IsType <JsonResult>(action); var jsonType = jsonResult.Data.GetType(); Assert.Contains("location", jsonType.GetProperties().Select(m => m.Name)); Assert.Equal(urlLocation, jsonType.GetProperty("location").GetValue(jsonResult.Data)); Assert.True(sut.ModelState.IsValid); }
public void IsAuthenticated_ReturnsFalse_GetUserId_GetUserName_ReturnsNull_WithNullUser() { var fixture = FixtureExtensions.CreateFixture(); // Arrange fixture.Register <IPrincipal>(() => null); var sut = fixture.Create <UserService>(); // Act bool isAuthenticated = sut.IsAuthenticated; string userId = sut.GetUserId(); string userName = sut.GetUserName(); // Assert Assert.False(isAuthenticated, "IsAuthenticated must be false for null IPrincipal."); Assert.Null(userId); Assert.Null(userName); }
public async Task GetUsersInRoleProfileAsync() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <UserProfileDomain.Models.UserProfile>()); // Arrange var testRole = fixture.Create <StandardRoles>(); var matchCount = fixture.Create <int>(); var count = fixture.Create <int>(); var expectedProfiles = fixture.CreateUsers(matchCount, () => fixture.CreateMany <StandardRoles>() .Concat(new[] { testRole }) .Distinct() .ToList() ).ToList(); var profileStore = expectedProfiles.Concat( fixture.CreateUsers(count, () => fixture.CreateMany <StandardRoles>() .Where(r => r != testRole) .Distinct() .ToList() ) ).ToList(); var upContext = fixture.FreezeDbContext <IUserProfileContext>(); MockingHelpers.MockDbContextSet(upContext, c => c.UserProfiles, profileStore.AsQueryable()); var sut = fixture.Create <UserProfileRepository>(); // Act var actualAsync = await sut.GetUsersInRoleProfileAsync(testRole); //Assert Assert.Equal(expectedProfiles, actualAsync); if (expectedProfiles.Any(p => p.User.Roles.Count > 1)) { Assert.Contains(actualAsync, p => p.User.Roles.Count > 1); } }
public void Index_AnonymousUser_ShouldDisplayLandingPage() { var fixture = FixtureExtensions.CreateFixture(); // Arrange var userService = fixture.Freeze <Mock <IUserService> >(); userService.Setup(i => i.IsAuthenticated).Returns(false); var storyRepository = fixture.Freeze <Mock <IStoryRepository> >(); var controller = fixture.CreateController <HomeController>(); // Act ViewResult result = controller.Index() as ViewResult; // Assert Assert.NotNull(result); Assert.Equal("", result.ViewName); }
public async Task FindUserByName_CallsUserManager() { var fixture = FixtureExtensions.CreateFixture(); // Arrange var userInfo = fixture.Create <UserInfo>(); var userMgr = fixture.Freeze <Mock <ApplicationUserManager> >(); userMgr.Setup(mgr => mgr.FindByNameAsync(It.IsAny <string>())).Returns(Task.FromResult(userInfo)); var sut = new UserService(userMgr.Object, null); // Act var actualUserInfo = await sut.FindUserByNameAsync(userInfo.UserName); // Assert userMgr.Verify(mgr => mgr.FindByNameAsync(It.Is <string>(_uName => _uName == userInfo.UserName))); Assert.Equal(userInfo, actualUserInfo); }
public void StoryComments_AreMappedTo_StoryVM() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Story>()); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Comment>()); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Group>()); fixture.Customizations.Add(new NavigationPropertyOmitter <Comment, Comment>()); // Arrange var comments = fixture.CreateMany <Comment>(2).ToList(); var likes = fixture.CreateMany <Like>(2).ToList(); var sut = fixture.Build <Story>() .With(x => x.Comments, comments) .With(x => x.Likes, likes) .Create(); // Act var storyVM = Mapper.Map <StoryVM>(sut); // Assert Assert.NotNull(storyVM.Comments); Assert.NotEmpty(storyVM.Comments); Assert.All(storyVM.Comments, commentVM => { var expectedComment = comments.First(c => c.Id == commentVM.Id); Assert.Equal(expectedComment.Text, commentVM.Text); Assert.Equal(expectedComment.UserId, commentVM.UserId); }); Assert.NotNull(storyVM.Likes); Assert.NotEmpty(storyVM.Likes); Assert.All(storyVM.Likes, likeVM => { var expectedLike = likes.First(c => c.Id == likeVM.Id); Assert.Equal(expectedLike.StoryId, likeVM.StoryId); Assert.Equal(expectedLike.Date, likeVM.Date); Assert.Equal(expectedLike.UserId, likeVM.UserId); }); }
public async Task UpdateProfile_TypeName_ShouldSave_TheUpdatedVM(string updateType) { var fixture = FixtureExtensions.CreateFixture(); AddCustomizations(fixture); //Arange var loggedInUserId = fixture.Create <string>(); var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(u => u.GetUserId()).Returns(loggedInUserId); var repositoryProfile = fixture.Build <UserProfile>() .With(p => p.UserId, loggedInUserId) .Create(); var originalProfile = new UserProfile(); mapper.Map(repositoryProfile, originalProfile, opts => opts.ConfigureMap()); var upUOF = fixture.Freeze <Mock <IUserProfileUnitOfWork> >(); upUOF.Setup(uof => uof.UserProfiles.GetUserProfileAsync(It.IsAny <string>())) .Returns(() => Task.FromResult(repositoryProfile)); var sut = fixture.CreateController <UserProfileController>(); var expectedVM = fixture.Build <UserProfileVM>() .With(profile => profile.UserId, loggedInUserId) .Create(); // Act var action = await sut.UpdateProfile(expectedVM, updateType); // Assert upUOF.Verify(r => r.UserProfiles.GetUserProfileAsync(It.IsAny <string>())); upUOF.Verify(r => r.CompleteAsync()); AssertUpdatedVMBasedOnUpdateType(originalProfile, expectedVM, repositoryProfile, updateType); }
public async Task AddInterest_ShouldRetrieve_CurrentUserProfile() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <UserProfile>()); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Group>()); // Arrange var currentUserId = fixture.Create <string>(); var currentUserProfile = fixture.Build <UserProfile>() .With(p => p.Interests, new List <UserInterest>()) .Create(); var group = fixture.Build <Group>() .With(g => g.Members, new List <GroupMember>()) .CreateMany(1) .ToList() as IList <Group>; var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.GetUserId()).Returns(currentUserId); var upRepo = fixture.Freeze <Mock <IUserProfileRepository> >(); upRepo.Setup(r => r.GetUserProfileAsync(It.IsAny <string>())) .Returns(Task.FromResult(currentUserProfile)); var storyUOW = fixture.Freeze <Mock <IStoryUnitOfWork> >(); storyUOW.Setup(uow => uow.Groups.GetAsync(It.IsAny <Expression <Func <Group, bool> >[]>())) .Returns(Task.FromResult(group)); var interestId = fixture.Create <int>(); var sut = fixture.CreateController <UserProfileController>(); // Act var view = await sut.AddInterest(interestId); // Assert upRepo.Verify(r => r.GetUserProfileAsync(It.Is <string>(i => i == currentUserId))); }
public void IsAuthenticated_ReturnsValueOf_UserIdentityIsAuthenticated(bool isAuthVal) { var fixture = FixtureExtensions.CreateFixture(); // Arrange var id = fixture.Freeze <Mock <IIdentity> >(); id.Setup(i => i.IsAuthenticated).Returns(isAuthVal); var usr = fixture.Freeze <Mock <IPrincipal> >(); usr.Setup(i => i.Identity).Returns(id.Object); var sut = fixture.Create <UserService>(); // Act bool isAuthenticated = sut.IsAuthenticated; // Assert Assert.Equal(isAuthVal, isAuthenticated); }
public void IsAuthenticated_ReturnsFalse_GetUserId_GetUserName_ReturnsNull_WithNullIdentity() { var fixture = FixtureExtensions.CreateFixture(); // Arrange var usr = fixture.Freeze <Mock <IPrincipal> >(); usr.Setup(i => i.Identity).Returns((IIdentity)null); var sut = fixture.Create <UserService>(); // Act bool isAuthenticated = sut.IsAuthenticated; string userId = sut.GetUserId(); string userName = sut.GetUserName(); // Assert Assert.False(isAuthenticated, "IsAuthenticated must be false for null IIdentity."); Assert.Null(userId); Assert.Null(userName); }
public async Task Index_ShouldReturnAn_UserProfileVM(string userName) { var fixture = FixtureExtensions.CreateFixture(); AddCustomizations(fixture); //Arange var userProfile = fixture.Create <UserProfile>(); var upRepo = fixture.Freeze <Mock <IUserProfileRepository> >(); upRepo.Setup(r => r.GetUserProfileAsync(It.IsAny <string>())).Returns(Task.FromResult(userProfile)); var sut = fixture.CreateController <UserProfileController>(); // Act var view = await sut.Index(userName) as ViewResult; // Assert Assert.IsType <UserProfileVM>(view.Model); }
public async Task UpdateProfile_ShouldReturn_IndexView() { var fixture = FixtureExtensions.CreateFixture(); AddCustomizations(fixture); //Arange var upRepo = fixture.Freeze <Mock <IUserProfileRepository> >(); var sut = fixture.CreateController <UserProfileController>(); var userProfileVM = fixture.Create <UserProfileVM>(); // Act var action = await sut.UpdateProfile(userProfileVM, null); var view = action as ViewResult; // Assert Assert.IsType <ViewResult>(action); Assert.Equal("Index", view.ViewName); }
public async Task Index_ShouldReturn_CorrectUserRole(StandardRoles role) { var fixture = FixtureExtensions.CreateFixture(); AddCustomizations(fixture); // Arrange var userName = fixture.Create <string>(); var uInfo = fixture.CreateUser( fixture.CreateMany <StandardRoles>() .Where(r => r < role) .Distinct() .Concat(new[] { role }) .ToList() ); var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.FindUserByNameAsync(It.IsAny <string>())) .Returns(Task.FromResult(uInfo)); var sut = fixture.CreateController <UserProfileController>(); // Act var view = await sut.Index(userName) as ViewResult; var model = view.Model as UserProfileVM; // Assert if (role == StandardRoles.Normal) { Assert.Equal("", model.Role); } else { Assert.Equal(role.ToString(), model.Role); } }
public async Task GrantRole_ShouldReturnError_IfFailsToAddUserToRole() { var fixture = FixtureExtensions.CreateFixture(); var grantRoleVM = fixture.Create <GrantRoleVM>(); var errorMsgs = fixture.CreateMany <string>().ToList(); var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.FindUserByIdAsync(It.IsAny <string>())) .Returns(Task.FromResult(new UserInfo())); uService.Setup(s => s.IsInRole(It.IsAny <string>(), It.IsAny <string>())) .Returns(false); uService.Setup(s => s.AddToRoleAsync(It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(GrantRoleResult.Failed(errorMsgs))); var sut = fixture.CreateController <AdminController>(); // Act var action = await sut.GrantRole(grantRoleVM); var pView = action as PartialViewResult; // Assert uService.Verify(s => s.IsInRole(It.Is <string>(v => v == grantRoleVM.UserId), It.Is <string>(v => v == grantRoleVM.RoleName))); Assert.IsType <PartialViewResult>(action); Assert.Equal("_AjaxValidation", pView.ViewName); Assert.False(sut.ModelState.IsValid); Assert.Contains("GrantRoleAction", sut.ModelState.Keys); ModelState actualErrMsgs = sut.ModelState["GrantRoleAction"]; Assert.Equal(errorMsgs.Count, actualErrMsgs.Errors.Count); Assert.Equal(errorMsgs, actualErrMsgs.Errors.Select(e => e.ErrorMessage)); }
public async Task Index_ShouldQueryUserStories(string userName, int numberOfStories) { var fixture = FixtureExtensions.CreateFixture(); AddCustomizations(fixture); // Arrange var userStories = fixture.CreateMany <Story>(numberOfStories).ToList(); var storyRepo = fixture.Freeze <Mock <IStoryRepository> >(); storyRepo.Setup(r => r.GetUserStoriesAsync(It.IsAny <string>())).Returns(Task.FromResult(userStories as IList <Story>)); var sut = fixture.CreateController <UserProfileController>(); // Act var view = await sut.Index(userName) as ViewResult; var model = view.Model as UserProfileVM; // Assert Assert.NotNull(model.Stories); if (!userStories.Any()) { Assert.Empty(model.Stories); } else { Assert.All(model.Stories, storyVM => { var expectedStory = userStories.First(s => s.Id == storyVM.Id); Assert.Equal(expectedStory.Title, storyVM.Title); Assert.Equal(expectedStory.User.UserName, storyVM.UserName); Assert.Equal(expectedStory.UserId, storyVM.UserId); Assert.Equal(expectedStory.User.Email, storyVM.UserEmail); }); } }
public async Task Index_CalledWithNoUserName_ShouldReturn_CurrentUserProfileVM() { var fixture = FixtureExtensions.CreateFixture(); AddCustomizations(fixture); //Arange var currentUserInfo = fixture.CreateUser(null); var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(u => u.GetUserName()).Returns(currentUserInfo.UserName); uService.Setup(u => u.FindUserByNameAsync(It.Is <string>(_userName => _userName == currentUserInfo.UserName))).Returns(Task.FromResult(currentUserInfo)); var currentUserProfile = fixture.Build <UserProfile>() .With(up => up.User, currentUserInfo) .With(up => up.UserId, currentUserInfo.Id) .Create(); var upRepo = fixture.Freeze <Mock <IUserProfileRepository> >(); upRepo.Setup(r => r.GetUserProfileAsync(It.Is <string>(_userId => _userId == currentUserInfo.Id))).Returns(Task.FromResult(currentUserProfile)); var sut = fixture.CreateController <UserProfileController>(); // Act var view = await sut.Index() as ViewResult; var model = view.Model as UserProfileVM; // Assert uService.Verify(u => u.GetUserName(), Times.Once()); uService.Verify(u => u.FindUserByNameAsync(It.Is <string>(_userName => _userName == currentUserInfo.UserName)), Times.Once(), "FindUserByName was not called or called with wrong parameter."); upRepo.Verify(r => r.GetUserProfileAsync(It.Is <string>(_userId => _userId == currentUserInfo.Id)), Times.Once(), "GetUserProfileAsync was not called or called with wrong parameter."); Assert.IsType <UserProfileVM>(view.Model); Assert.Equal(currentUserProfile.User.UserName, model.UserName); Assert.Equal(currentUserProfile.UserId, model.UserId); }
public void Group_MapsTo_GroupVM() { var fixture = FixtureExtensions.CreateFixture(); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <UserProfileDomain.Models.UserProfile>()); fixture.Customizations.Add(new ManyNavigationPropertyOmitter <Group>()); // Arrange var groups = fixture.Build <Group>() .With(g => g.Members, fixture.CreateMany <GroupMember>().ToList()) .CreateMany() .ToList(); // Act var groupVM = Mapper.Map <IList <GroupVM> >(groups); // Assert Assert.Equal(groups.Count, groupVM.Count); var joinedGroups = groupVM.Join(groups, gvm => gvm.Id, g => g.Id, (gvm, g) => new { gvm, g }).ToList(); Assert.Equal(groups.Count, joinedGroups.Count); Assert.All(joinedGroups, jGroup => { Assert.Equal(jGroup.g.Title, jGroup.gvm.Title); Assert.Equal(jGroup.g.Members.Count, jGroup.gvm.Members.Count); var joinedMembers = jGroup.gvm.Members.Join(jGroup.g.Members, up => up.Id, gm => gm.UserProfile.Id, (up, gm) => new { up, gm }).ToList(); Assert.Equal(jGroup.g.Members.Count, joinedMembers.Count); Assert.All(joinedMembers, jMember => { Assert.Equal(jMember.gm.UserProfile.FirstName, jMember.up.FirstName); Assert.Equal(jMember.gm.UserProfile.LastName, jMember.up.LastName); }); }); }
public async Task UpdateProfile_WithoutValidUser_ShouldReturn_InvalidModelState_ViewResult(string currentUserId, string savedUserId, string updateType) { var fixture = FixtureExtensions.CreateFixture(); AddCustomizations(fixture); //Arange var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.GetUserId()).Returns(currentUserId); var sut = fixture.CreateController <UserProfileController>(); var userProfileVM = fixture.Build <UserProfileVM>() .With(p => p.UserId, savedUserId) .Create(); Enum.TryParse <UserProfileUpdateType>(updateType, out var uType); // Act var action = await sut.UpdateProfile(userProfileVM, updateType); var view = action as ViewResult; // Assert Assert.IsType <ViewResult>(action); Assert.Equal("Index", view.ViewName); Assert.False(sut.ModelState.IsValid); if (uType != UserProfileUpdateType.Unknown) { Assert.Contains("UserId", sut.ModelState.Keys); } else { Assert.Contains("updateType", sut.ModelState.Keys); } }