public void GetUser_RunServerAddUserList_GetUser() { DoIntegrationTest(async(client, dbContextOptions) => { //Arrange MiniAutoFixture miniAutoFixture = new MiniAutoFixture(); miniAutoFixture.Initialize(); List <User> userList = DbSetTools.AddExampleUsersToDatabase(dbContextOptions, miniAutoFixture); User userExample = userList[0]; UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture); var request = new HttpRequestMessage(new HttpMethod("GET"), string.Format("/api/user/{0}", userExample.Id)); //Action var response = await client.SendAsync(request); //Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); var content = await response.Content.ReadAsStringAsync(); UserView userView = JsonConvert.DeserializeObject <UserView>(content); Assert.Equal(userExample.Id, userView.Id); Assert.Equal(userExample.Name, userView.Name); Assert.Equal(userExample.Surname, userView.Surname); Assert.Equal(userExample.Deleted, userView.Deleted); }); }
public void InsertUser_MockDbSetUser_AndGetInsertUser() { //assign int newIdUser = 11; MiniAutoFixture miniAutoFixture = new MiniAutoFixture(); miniAutoFixture.Initialize(); List <User> usersCollection = DbSetTools.CreateUserList(1, 10, miniAutoFixture); IQueryable <User> users = usersCollection.AsQueryable(); var mockDbSet = new Mock <DbSet <User> >(); mockDbSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider); mockDbSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression); mockDbSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType); mockDbSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator()); mockDbSet.Setup(m => m.Add(It.IsAny <User>())) .Callback <User>(a => { a.Id = newIdUser; usersCollection.Add(a); }); var mockContext = new Mock <OrganizerContext>(); mockContext.Setup(c => c.User).Returns(mockDbSet.Object); var logger = new Mock <ILogger <ModifyRepository> >(); ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object); UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture); //action UserView userView = modifyRepository.InsertUser(userModify); //assert mockDbSet.Verify ( a => a.Add ( It.Is <User> ( user => user.Name == userModify.Name && user.Surname == userModify.Surname && !user.Deleted && user.Id == newIdUser ) ), Times.Once ); mockContext.Verify(a => a.SaveChanges(), Times.Once); Assert.Equal(userModify.Name, userView.Name); Assert.Equal(userModify.Surname, userView.Surname); Assert.Equal(false, userView.Deleted); Assert.Equal(newIdUser, userView.Id); }
public void Put_MockUserRepository_AndUpdateUser() { //assign MiniAutoFixture miniAutoFixture = new MiniAutoFixture(); miniAutoFixture.Initialize(); int idUser = miniAutoFixture.CreateInt(); miniAutoFixture.Initialize(); UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture); UserView userView = new UserView { Id = idUser, Name = userModify.Name, Surname = userModify.Surname, Deleted = miniAutoFixture.CreateBoolean() }; var viewRepository = new Mock <IViewRepository>(); var modifyRepository = new Mock <IModifyRepository>(); var logger = new Mock <ILogger <UserController> >(); modifyRepository.Setup(a => a.UpdateUser(It.IsAny <int>(), It.IsAny <UserModify>())).Returns <int, UserModify>((a, b) => userView); UserController userController = new UserController(viewRepository.Object, modifyRepository.Object, logger.Object); //action IActionResult actionResult = userController.Put(idUser, userModify); //assert modifyRepository.Verify ( a => a.UpdateUser ( It.Is <int>(val => val == idUser), It.Is <UserModify> (val => val.Name == userModify.Name && val.Surname == userModify.Surname) ) , Times.Once ); var okResult = Assert.IsType <OkObjectResult>(actionResult); var returnUser = Assert.IsType <UserView>(okResult.Value); Assert.NotNull(returnUser); Assert.Equal(userModify.Name, returnUser.Name); Assert.Equal(userModify.Surname, returnUser.Surname); Assert.Equal(idUser, returnUser.Id); Assert.Equal(userView.Deleted, returnUser.Deleted); }
public void ModifytUser_MockDbSetUser_AndGetUpdateUser() { //assign int idUserUpdate = 1; MiniAutoFixture miniAutoFixture = new MiniAutoFixture(); miniAutoFixture.Initialize(); List <User> usersCollection = DbSetTools.CreateUserList(1, 10, miniAutoFixture); IQueryable <User> users = usersCollection.AsQueryable(); UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture); var mockDbSet = new Mock <DbSet <User> >(); mockDbSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider); mockDbSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression); mockDbSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType); mockDbSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator()); var mockContext = new Mock <OrganizerContext>(); mockContext.Setup(c => c.User).Returns(mockDbSet.Object); var logger = new Mock <ILogger <ModifyRepository> >(); ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object); //action UserView userView = modifyRepository.UpdateUser(idUserUpdate, userModify); //assert User userModified = usersCollection.Where(a => a.Id == idUserUpdate).Single(); mockContext.Verify(a => a.Attach(It.Is <User>(user => userModified == user)), Times.Once); mockContext.Verify(a => a.SaveChanges(), Times.Once); Assert.Equal(userModify.Name, userView.Name); Assert.Equal(userModify.Surname, userView.Surname); Assert.Equal(userModified.Deleted, userView.Deleted); Assert.Equal(idUserUpdate, userView.Id); }
public void PostUser_RunServerAddUserList_GetAddUser() { DoIntegrationTest(async(client, dbContextOptions) => { //Arrange MiniAutoFixture miniAutoFixture = new MiniAutoFixture(); miniAutoFixture.Initialize(); List <User> userList = DbSetTools.AddExampleUsersToDatabase(dbContextOptions, miniAutoFixture); UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture); //Action var response = await client.PostAsJsonAsync("/api/user", userModify); //Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); var content = await response.Content.ReadAsStringAsync(); UserView userView = JsonConvert.DeserializeObject <UserView>(content); Assert.Equal(userModify.Name, userView.Name); Assert.Equal(userModify.Surname, userView.Surname); Assert.Equal(false, userView.Deleted); Assert.Equal(true, userView.Id > 0); }); }