Can I create a mock db context which I can add to, then search?

You can set up your mock DbSet's Add() method with a callback which will add the item in to your backing List:

private static Mock<DbSet<Document>> GetQueryableMockDocumentDbSet()
{
    var data = new List<Document> { GetDocument(111, 11), GetDocument(222, 22), GetDocument(333, 33) };

    var mockDocumentDbSet = new Mock<DbSet<Document>>();
    mockDocumentDbSet.As<IQueryable<Document>>().Setup(m => m.Provider).Returns(data.AsQueryable().Provider);
    mockDocumentDbSet.As<IQueryable<Document>>().Setup(m => m.Expression).Returns(data.AsQueryable().Expression);
    mockDocumentDbSet.As<IQueryable<Document>>().Setup(m => m.ElementType).Returns(data.AsQueryable().ElementType);
    mockDocumentDbSet.As<IQueryable<Document>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
    mockDocumentDbSet.Setup(m => m.Add(It.IsAny<Document>())).Callback<Document>(data.Add);
    return mockDocumentDbSet;

}

Your subsequent call to First() should then be able to retrieve the item.


Consider mocking out at a higher abstraction layer. In this case, consider mocking out the Respository. You could go even higer and mock out the service itself.

Construct testable business layer logic