chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male

Mocking an EF DBSet

0 votes
Hello, I am trying to write a simple unit test to get me started with TypeMock and am 
having trouble figuring out how to mock an EF DBSet. I have looked at the "Mocking DBSet"
question and did not actually see a clear answer so I am posting my own question. 

I am trying to test the "GetReportDefinitionComboBoxItemsByReportType" method below, and 
in doing so I will need to mock the dbContext.ReportDefinitionEntities. I have pasted 
what I have tried below the function that I am trying to test so you can see what I am 
doing. When I run this test, I get a System.NullReferenceExcpetion, I am guessing on the

What am I doing wrong?

Public Function GetReportDefinitionComboBoxItemsByReportType(ByVal reportType As ReportTypes) As List(Of StandardComboBoxItem)

 Using dbContext As New MyDataContext(_connectionString)

  ' ReSharper disable once AccessToDisposedClosure
  Dim query = From reportDefinitionEntity In dbContext.ReportDefinitionEntities
     Where reportDefinitionEntity.ReportType = reportType
     Order By reportDefinitionEntity.Name
     Select New StandardComboBoxItem With
     .DisplayText = reportDefinitionEntity.Name,
     .Value = reportDefinitionEntity.ReportDefinitionId

  Return query.ToList()

 End Using

End Function

Public Sub GetReportDefinitionComboBoxItemsByReportType()

 _fakeDbContext = Isolate.Fake.AllInstances(Of MyDataContext)()
 _reportsDal = New ReportsDal()

 Dim entities As DbSet(Of ReportDefinitionEntity) = _fakeDbContext.Set(Of ReportDefinitionEntity)()

 entities.Add(New ReportDefinitionEntity() With {.Name = "Name C", .ReportDefinitionId = 3, .ReportType = ReportTypes.BusinessRegistrationForm})
 entities.Add(New ReportDefinitionEntity() With {.Name = "Name A", .ReportDefinitionId = 2, .ReportType = ReportTypes.BusinessRegistrationForm})
 entities.Add(New ReportDefinitionEntity() With {.Name = "Name B", .ReportDefinitionId = 1, .ReportType = ReportTypes.BusinessRegistrationForm})

 Isolate.WhenCalled(Function() _fakeDbContext.ReportDefinitionEntities).WillReturnCollectionValuesOf(entities)
 Dim results = _reportsDal.GetReportDefinitionComboBoxItemsByReportType(ReportTypes.BusinessRegistrationForm)

 Assert.AreEqual(results.Count, 3)

End Sub


Thank you!

asked Aug 10, 2015 by donniedarko (21,340 points)
Even trying to test a simple method like this:

Dim query = From reportDefinitionEntity In dbContext.ReportDefinitionEntities
                            Select reportDefinitionEntity


Gives me an "Object reference not set to an instance of an objec." error. I can hover over dbContext and see all of the empty DBSets and I can see that the ReportDefinitionEntitites DBSet is filled with the list from my test method, but it still gives this exception and it looks like it is being thrown on dbContext.ReportDefinitionEntities.Count internally.



1 Answer

+2 votes
Best answer
 Ok, I figured it out. I just needed to add the "AsQueryable" when calling 

Isolate.WhenCalled(Function() _fakeDbContext.ReportDefinitionEntities) _




answered Aug 10, 2015 by donniedarko (21,340 points)