Created
January 9, 2017 20:55
-
-
Save mizrael/3668d6163b2b577c88cb3381abaf451b to your computer and use it in GitHub Desktop.
using Moq to mock a Repository and test an user update operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xunit; | |
using Moq; | |
public class UpdateUserHandlerTests | |
{ | |
[Fact] | |
public async Task should_Update_user_when_command_valid() | |
{ | |
var command = new UpdateUser(id: Guid.NewGuid(), firstname: "lorem", lastname: "ipsum"); | |
var user = new Infrastructure.Entities.User() | |
{ | |
Id = command.UserId | |
}; | |
var mockRepo = new Mock<IRepository<Infrastructure.Entities.UserData>>(); | |
mockRepo.Setup(r => r.FindOneAsync(It.IsAny<Expression<Func<Infrastructure.Entities.UserData, bool>>>())) | |
.ReturnsAsync(user); | |
var mockDbContext = new Mock<IUserServiceDbContext>(); | |
mockDbContext.Setup(db => db.UsersData).Returns(mockRepo.Object); | |
var sut = new UpdateUserHandler(mockDbContext.Object); | |
await sut.Handle(command); | |
mockRepo.Verify(m => m.UpsertOneAsync(It.IsAny<Expression<Func<Infrastructure.Entities.UserData, bool>>>(), It.IsAny<Infrastructure.Entities.UserData>()), Times.Once()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment