public void WhenCallingGetOrCreateUserWithNoMatchingUser_ThenUserIsCreated() { var userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(u => u.Create(It.IsAny<MileageStats.Model.User>())) .Callback<MileageStats.Model.User>(user => user.UserId = 12); Model.User userToReturn = null; userRepositoryMock.Setup(u => u.GetByAuthenticatedId(It.IsAny<string>())).Returns(userToReturn); userRepositoryMock.Setup(u => u.Create(It.IsAny<Model.User>())) .Callback<MileageStats.Model.User>(user => user.UserId = 12); var services = new UserServices(userRepositoryMock.Object); User createdUser = services.GetOrCreateUser("claimedId"); userRepositoryMock.Verify(u => u.Create(It.IsAny<Model.User>()), Times.Once()); }
public void WhenCallingGetOrCreateUserWithMatchingUser_ThenExistingUserIsReturned() { var userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(u => u.Create(It.IsAny<MileageStats.Model.User>())) .Callback<MileageStats.Model.User>(user => user.UserId = 12); string claimedId = "claimedId"; Model.User userToReturn = new Model.User() {AuthorizationId = claimedId}; userRepositoryMock.Setup(u => u.GetByAuthenticatedId(It.IsAny<string>())).Returns(userToReturn); var services = new UserServices(userRepositoryMock.Object); User createdUser = services.GetOrCreateUser(claimedId); userRepositoryMock.Verify(u => u.Create(It.IsAny<Model.User>()), Times.Never()); Assert.Equal(claimedId, createdUser.AuthorizationId); }