Last active
January 9, 2017 20:56
-
-
Save mizrael/ff98c513bf3a99322b7d2d12d7df9bef to your computer and use it in GitHub Desktop.
using Moq to mock a Repository and test a user create 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 CreateUserHandlerTests | |
{ | |
[Fact] | |
public async Task should_create_user_when_command_valid() | |
{ | |
var command = new CreateUser(id: Guid.NewGuid(), username: "loremipsum", email: "lorem@ipsum.com", firstname: "lorem", lastname:"ipsum"); | |
var mockRepo = new Mock<IRepository<Infrastructure.Entities.User>>(); | |
var mockDbContext = new Mock<IDbContext>(); | |
mockDbContext.Setup(db => db.Users).Returns(mockRepo.Object); | |
var sut = new CreateUserHandler(mockDbContext.Object); | |
await sut.Handle(command); | |
mockRepo.Verify(m => m.InsertOneAsync(It.IsAny<Infrastructure.Entities.User>()), Times.Once()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment