lundi 30 novembre 2015

Unit testing static methods using moq issue

I am having trouble understanding why there is discrepancy between these two tests.

First, Test A:

Public Sub GetTypes_Success()
    _context = New Mock(Of IFraternalEntities)()
    _cacheProvider = New Mock(Of ICacheProvider)()

    Dim type1 = EntityCreator.Create(Of SomeType)()
    Dim type2 = EntityCreator.Create(Of SomeType)()
    Dim listOfTypes = New List(Of SomeType)({type1, type2})
    _context.Setup(Function(r) r.SomeTypes).Returns(QueryableMockSet(Of SomeType).Create(listOfTypes.AsQueryable()).Object)

    _repo = New TypeRepo(_context.Object, _cacheProvider.Object)
    Dim results = _repo.GetSomeTypes()

    Assert.AreEqual(listOfTypes.Count, results.Count, "Not all types returned")
End Sub

testing the method

Public Function GetSomeTypes() As IEnumerable(Of DTO.SomeType) Implements ITypeRepo.GetSomeTypes
    Dim results = Entities.SomeTypes.OrderBy(Function(x) x.SortOrder).AsEnumerable()
    Return results.Select(Function(x) New DTO.SomeType With {
            .TypeID = x.TypeID,
            .Name = x.Name,
            .Description = x.Description
    })
End Function

This test works perfectly. However, I have a dang near equal test here, Test B:

Public Sub GetSecondaryTypes_Success()
    _context = New Mock(Of IFraternalEntities)()
    _cacheProvider = New Mock(Of ICacheProvider)()

    Dim type1 = EntityCreator.Create(Of OtherType)()
    Dim type2 = EntityCreator.Create(Of OtherType)()
    Dim listOfOtherTypes = New List(Of OtherTypes)({type1, type2})
    _context.Setup(Function(r) r.OtherTypes).Returns(QueryableMockSet(Of OtherType).Create(listOfOtherTypes.AsQueryable()).Object)
    _repo = New TypeRepo(_context.Object, _cacheProvider.Object)
    Dim results = _repo.GetOtherTypes()
    Assert.AreEqual(listOfOtherTypes.Count, results.Count, "Not all types returned")
End Sub

Testing this method

Public Function GetOtherTypes() As IEnumerable(Of DTO.OtherType) Implements ITypeRepo.GetOtherTypes
    Dim results = Entities.OtherTypes.AsEnumerable()
    Return results.Select(Function(x) x.MapToDTO())
End Function

with the MapToDTO() looking like this:

<Extension>
    Function MapToDTO(entity As NaturalDisaster) As DTO.NaturalDisaster
        Dim dto = New DTO.NaturalDisaster With {
            .TypeID= entity.TypeID,
            ''lots of other properties here
        }
        Return dto
    End Function

I understand that the Select causes the error NotSupportedException: expression references a method that does not belong to the mocked object. I also understand that doing

_context.setup(function(r) r.OtherTypes.AsEnumerable()).Returns(''this stuff here)

does not work because at this point we are dealing with a linq query. Can someone give me some insight on to why the first method works but the second method does not?

In advance, I may have made an error when transcribing my code onto here, so if it is a syntax error I will correct it.

Thanks

Aucun commentaire:

Enregistrer un commentaire