Sample 3: Testing async queries
Original Method
The following sample shows that In order to use asynchronous queries we don't need to change much just add
the word "async" to the test declaration and when we call the tested method to use the word "await":
C# public async Task<List<Blog>> GetAllBlogsAsync() { using (var database = new BloggingContext()) { var query = from b in database.Blogs orderby b.Name select b; return await query.ToListAsync(); } }
VB
Public Async Function GetAllBlogsAsync() As Task(Of List(Of Blog))
Using database = New BloggingContext()
Dim query = From b In database.Blogs
Order By b.Name Select b
Return Await query.ToListAsync()
End Using
End Function
Typemock Isolator Features Used
• Swap Future Instance
• Replacing Collections
• Changing property behavior
• Faking Linq
• No changes needed for testing asynchronous queries with TypeMock
Scenario
1. Get a handle for future BloggingContext class with its original methods.
2. Create a fake list with three new Blog Entities with different names.
3. Fake the BloggingContext.Blogs property to return our fake list As Queryable so we can use SQL commands on it.
4. Call the Program.GetAllBlogs method.
5. Check if the list is sorted and if it has the required amount of Blogs in it.
Code
C# [TestMethod, Isolated] public async void GetFakedListOfAllEntitiesAsync_SortAndReturnTheList() { List<Blog> fakeDb = new List<Blog>(); //Get a handle for future instance of BloggingContext that will be created var fake = Isolate.Fake.NextInstance<BloggingContext>(Members.CallOriginal); //Fill the List with fake Blogs fakeDb.Add (new Blog() { Name = "TypeMockRocks" }); fakeDb.Add (new Blog() { Name = "TheBestMockIsTypMock" }); fakeDb.Add (new Blog() { Name = "Mocking101" }); //Changing the behavior of the proprety BloggingContext.Blogs Isolate.WhenCalled(() => fake.Blogs).WillReturnCollectionValuesOf(fakeDb.AsQueryable()); //Call the method that is under test with "await" var ut = new Program(); List<Blog> result = await ut.GetAllBlogsAsync(); //Assert Assert.AreEqual(3, result.Count); Assert.AreEqual("Mocking101", result[0].Name); Assert.AreEqual("TheBestMockIsTypMock", result[1].Name); Assert.AreEqual("TypeMockRocks", result[2].Name); }
VB
<TestMethod(), Isolated()>
Public Async Sub GetFakedListOfAllEntitiesAsync_SortAndReturnTheList()
Dim fakeDb As List(Of Blog) = New List(Of Blog)()
'Get a handle for future instance of BloggingContext that will be created
Dim fake = Isolate.Fake.NextInstance(Of BloggingContext)(Members.CallOriginal)
'Fill the List with fake Blogs
fakeDb.Add(New Blog() With {
.Name = "TypeMockRocks"
})
fakeDb.Add(New Blog() With {
.Name = "TheBestMockIsTypMock"
})
fakeDb.Add(New Blog() With {
.Name = "Mocking101"
})
'Changing the behavior of the proprety BloggingContext.Blogs
Isolate.WhenCalled(Function() fake.Blogs).WillReturnCollectionValuesOf(fakeDb.AsQueryable())
'Call the method that is under test with "await"
Dim ut = New Program()
Dim result As List(Of Blog) = Await ut.GetAllBlogsAsync()
'Assert
Assert.AreEqual("Mocking101", result(0).Name)
Assert.AreEqual("TheBestMockIsTypMock", result(1).Name)
Assert.AreEqual("TypeMockRocks", result(2).Name)
Assert.AreEqual(3, result.Count)
End Sub