Skip to content

Instantly share code, notes, and snippets.

@taylorc
Last active January 9, 2020 04:14
Show Gist options
  • Save taylorc/040f92de699bf3955cb82bce52dc5089 to your computer and use it in GitHub Desktop.
Save taylorc/040f92de699bf3955cb82bce52dc5089 to your computer and use it in GitHub Desktop.
[Collection("QueryTests")]
public class GetTodosQueryTests {
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
public GetTodosQueryTests(QueryTestFixture fixture) {
_mapper = fixture.Mapper;
}
[Fact]
public async Task Handle_ReturnsCorrectVmAndListCount() {
//arrange
var query = new GetTodosQuery();
var mockedDbContext = new Mock < IApplicationDbContext > ();
var todoListEntities = new List < TodoList > {
new TodoList {
Colour = "Blue", Items = new List < TodoItem > {
new TodoItem {
Title = "Dynamics"
},
new TodoItem {
Title = "Case"
},
new TodoItem {
Title = "Management"
},
new TodoItem {
Title = "Accelerator"
},
new TodoItem {
Title = "HSD"
}
}
}
};
var mockDbSet = todoListEntities.AsQueryable().BuildMockDbSet();
mockedDbContext.Setup(db => db.TodoLists).Returns(mockDbSet.Object);
//act
var handler = new GetTodosQuery.GetTodosQueryHandler(mockedDbContext.Object, _mapper);
var result = await handler.Handle(query, CancellationToken.None);
//assert
result.ShouldBeOfType < TodosVm > ();
result.Lists.Count.ShouldBe(1);
var list = result.Lists.First();
list.Items.Count.ShouldBe(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment