public void TestAddingUserSameName() { string originalDisplayName = "Some display name"; User user = new User() { DisplayName = originalDisplayName }; using (ApiContext context = new ApiContext(dbOptions)) { UserService service = new UserService(context); service.Add(user); } using (ApiContext context = new ApiContext(dbOptions)) { UserService service = new UserService(context); Assert.Catch <DuplicateEntryException>(() => service.Add(user)); // Test with different casing user.DisplayName = "some Display Name"; Assert.Catch <DuplicateEntryException>(() => service.Add(user)); user.DisplayName = " " + user.DisplayName + " "; Assert.Catch <DuplicateEntryException>(() => service.Add(user)); } // Make sure the user was added to the DB using (ApiContext context = new ApiContext(dbOptions)) { Assert.AreEqual(1, context.Users.Count()); Assert.IsTrue(context.Users.Any(u => u.DisplayName == originalDisplayName)); } }
public void TestAddingUserNoName() { User user = new User() { DisplayName = "" }; using (ApiContext context = new ApiContext(dbOptions)) { UserService service = new UserService(context); // Test with name as an empty string Assert.Catch <System.ArgumentException>(() => service.Add(user)); // Test with a name of just white space user.DisplayName = " "; Assert.Catch <System.ArgumentException>(() => service.Add(user)); user.DisplayName = null; Assert.Catch <System.ArgumentException>(() => service.Add(user)); } // Make sure no users are in the DB using (ApiContext context = new ApiContext(dbOptions)) { Assert.AreEqual(0, context.Users.Count()); } }
public void add_should_not_add_existing_user() { _userRepository.GetUserBy("aUser").Returns(new User { UserName = "******" }); var user = _service.Add(new User { UserName = "******" }); user.Should().BeNull(); }
public void TestAddUserExistingId() { User user = new User() { DisplayName = "Some display name" }; User user2 = new User() { DisplayName = "Some other display name" }; using (ApiContext context = new ApiContext(dbOptions)) { context.Users.Add(user); context.SaveChanges(); } // Set the second user to have a duplicate identifier user2.Id = user.Id; using (ApiContext context = new ApiContext(dbOptions)) { UserService service = new UserService(context); service.Add(user2); } // Make sure 2 animals are in the DB and are correct using (ApiContext context = new ApiContext(dbOptions)) { Assert.AreEqual(2, context.Users.Count()); Assert.AreEqual(user.Id, context.Users.First().Id); Assert.AreEqual(user.DisplayName, context.Users.First().DisplayName); Assert.AreEqual(user2.Id, context.Users.Last().Id); Assert.AreEqual(user2.DisplayName, context.Users.Last().DisplayName); } }
public void TestAddingUser() { User user = new User() { DisplayName = "Some display name" }; using (ApiContext context = new ApiContext(dbOptions)) { UserService service = new UserService(context); service.Add(user); } // Make sure the user was added to the DB using (ApiContext context = new ApiContext(dbOptions)) { Assert.AreEqual(1, context.Users.Count()); Assert.AreNotEqual(0, context.Users.First().Id); Assert.IsTrue(context.Users.Any(u => u.DisplayName == user.DisplayName)); } // Make sure that white space is trimmed string newUserName = "******"; using (ApiContext context = new ApiContext(dbOptions)) { UserService service = new UserService(context); User user2 = new User() { DisplayName = newUserName }; service.Add(user2); } // Make sure the user was added to the DB and auto-increment works using (ApiContext context = new ApiContext(dbOptions)) { Assert.AreEqual(2, context.Users.Count()); Assert.AreNotEqual(0, context.Users.Skip(1).First().Id); Assert.IsTrue(context.Users.Any(u => u.DisplayName == newUserName.Trim())); } }