public void WhenCreatingUser_ThenDelegatesToUserRepository()
        {
            var userRepositoryMock = new Mock<IUserRepository>();

            var services = new UserServices(userRepositoryMock.Object);

            services.CreateUser(new User() {AuthorizationId = "authId"});

            userRepositoryMock.Verify(r => r.Create(It.IsAny<MileageStats.Model.User>()), Times.Once());
        }
        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);
        }
        public void WhenUpdatingUser_ThenDelegatesToUserRepository()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(u => u.Create(It.IsAny<MileageStats.Model.User>()))
                .Callback<MileageStats.Model.User>(u => u.UserId = 12);

            var services = new UserServices(userRepositoryMock.Object);

            User user = new User() {AuthorizationId = "authId"};
            services.CreateUser(user);
            user.AuthorizationId = "newAuthId";
            services.UpdateUser(user);

            userRepositoryMock.Verify(r => r.Update(It.IsAny<MileageStats.Model.User>()), Times.Once());
        }