public async Task AddProfileForUser_Failure_ReturnABadRequest(string username, string fullname, string address1, string address2, string photoPublicId, string PhotoURL, string City, string State, string Zipcode) { // Arrange : set up a new user profile Mock <IUserRepository> mockIUserRepository = new Mock <IUserRepository>(); Mock <IMapper> mockIMapper = new Mock <IMapper>(); Mock <IPhotoUploadService> mockICloudinary = new Mock <IPhotoUploadService>(); // return an assigned user when call a mock version of repo var userWithProfile = GetUserWithProfile(username); //User can only add profile once login so repo must return a user mockIUserRepository.Setup(user_repo => user_repo.GetUser(It.IsAny <string>())) .Returns(Task.FromResult(AuthControllerTest.GetUser(username, "password"))); var profileForCreation = GetClientProfile(); mockIMapper.Setup(_mapper => _mapper.Map <ClientProfile>(It.IsAny <ProfileForCreationDto>())) .Returns(profileForCreation); //This false => no changes are made to repo mockIUserRepository.Setup(user_repo => user_repo.SaveAll()).Returns(Task.FromResult(false)); var profileController = new ProfileController(mockIUserRepository.Object, mockIMapper.Object, mockICloudinary.Object); //Action: Call AddProfileForUser in API var response = await profileController.AddProfileForUser(username, new ProfileForCreationDto() { }); //Assert: Expect A Bad Request ("Could not add the project") of type Bad Reqest Object Result Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.BadRequestObjectResult>(response); }
private User GetUserWithProfile(string username) { var user = AuthControllerTest.GetUser(username, "password"); user.ClientProfile = GetClientProfile(); return(user); }
// Create a virtual users data for unit UsersController API method test purpose private IEnumerable <User> GetUsers() { List <User> list_users = new List <User>(); list_users.Add(AuthControllerTest.GetUser("lewis", "password")); list_users.Add(AuthControllerTest.GetUser("bob", "password")); IEnumerable <User> users = list_users; return(users); }
public async Task GetUserTest_ReturnOneUser(string username) { //Arrange: create concrete dependency injection and an instance of UsersController Mock <IUserRepository> mockIUserRepository = new Mock <IUserRepository>(); Mock <IMapper> mockIMapper = new Mock <IMapper>(); // Return a virtual user when query mockIUserRepository.Setup(user_repo => user_repo.GetUser(username)). ReturnsAsync(AuthControllerTest.GetUser(username, "password")); // Mapping virtual object when calling mapper var usersController = new UsersController(mockIUserRepository.Object, mockIMapper.Object); //Action: Call GetUser to get the user with the matching username and expect an ok status code from Users API Controller var response = await usersController.GetUser(username); //Assert: Check if actual response is expected: an ok object result Ok(user) Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.OkObjectResult>(response); }
public async Task AddProfileForUser_Success_ReturnAnOkStatusWithNewProfile(string username, string fullname, string address1, string address2, string photoPublicId, string PhotoURL, string City, string State, string Zipcode) { // Arrange : set up a new user profile Mock <IUserRepository> mockIUserRepository = new Mock <IUserRepository>(); Mock <IMapper> mockIMapper = new Mock <IMapper>(); Mock <IPhotoUploadService> mockICloudinary = new Mock <IPhotoUploadService>(); // return an assigned user when call a mock version of repo var userWithProfile = GetUserWithProfile(username); var profileForCreation = GetClientProfile(); mockIUserRepository.Setup(user_repo => user_repo.GetUser(It.IsAny <string>())) .Returns(Task.FromResult(AuthControllerTest.GetUser(username, "password"))); mockIMapper.Setup(_mapper => _mapper.Map <ClientProfile>(It.IsAny <ProfileForCreationDto>())) .Returns(profileForCreation); //This true means there a changed in db => new user profile edited successfully mockIUserRepository.Setup(user_repo => user_repo.SaveAll()).Returns(Task.FromResult(true)); mockIMapper.Setup(_mapper => _mapper.Map <ProfileForReturnDto>(userWithProfile.ClientProfile)) .Returns(new ProfileForReturnDto() { }); // Inject Profile Controller dependency var profileController = new ProfileController(mockIUserRepository.Object, mockIMapper.Object, mockICloudinary.Object); var response = await profileController.AddProfileForUser(username, new ProfileForCreationDto() { Fullname = fullname, Address1 = address1, Address2 = address2, PhotoPublicId = photoPublicId, PhotoURL = PhotoURL, City = City, State = State, Zipcode = Zipcode }); // Assert: Test Acutal vs Expect Ok(ProfileUser) object Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.OkObjectResult>(response); }