fsprojects / Foq

A unit testing framework for F#
http://www.slideshare.net/ptrelford/foq-17062247
Apache License 2.0
79 stars 31 forks source link

Issues asserting on constrained types #28

Closed Ryan-Palmer closed 5 years ago

Ryan-Palmer commented 5 years ago

Description

any() comparison fails with constrained generics

Repro steps

I am trying to set up a method on my mock EntityFramework context:

member this.UpdateExisting<'a when 'a: not struct> (existing, updated) = base.Entry<'a>(existing).CurrentValues.SetValues(updated)

my mocked context is set up as so ( I have to use Moq for the DbSet as Foq's type checking is too strict) :

let dtos : Moq.Mock<DbSet<SurveyDto>> = [] |> Helpers.getQueryableMockDbSet

let dbContext = Mock<IDatabaseContext>() .Setup(fun ctx -> <@ ctx.Surveys @>) .Returns(dtos.Object) .Create()

I try to assert like this, including a type: verify <@dbContext.Add<SurveyDto>(any())@> once or like this without one: verify <@dbContext.Add(any())@> once

Either way I get a failure with "System.TypeLoadException : GenericArguments[0], 'a', on 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[TEntity]' violates the constraint of type parameter 'TEntity'."

Ryan-Palmer commented 5 years ago

Ah - I realised this is because I had imported the Moq F# extensions and I was using the wrong 'any()' function.