Sample 2: Testing queries
Original Method

The following sample shows a method that collects all Entities from BloggingContext and returns it as a list:

C#

public static List<Blog> GetAllBlogs()
{
  using (var database = new BloggingContext())
  {
    //Display all Blogs sorted from the database 
    var query = from b in database.Blogs
                orderby b.Name
                select b;
    
    return query.ToList();
  }
}

VB
Public Shared Function GetAllBlogs() As List(Of Blog)
        Using database = New BloggingContext()
            'Display all Blogs sorted from the database 
            Dim query = From b In database.Blogs
                        Order By b.Name Select b

            Return query.ToList()
        End Using
    End Function

Typemock Isolator Features Used

Swap Future Instance

Replacing Collections

Changing property behavior

Faking Linq

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 Linq 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 void GetFakedListOfAllEntities_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
  List<Blog> result = Program.GetAllBlogs();

  //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 Sub GetFakedListOfAllEntities_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
    Dim result As List(Of Blog) = Program.GetAllBlogs()

    '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