huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute
MIT License
132 stars 24 forks source link

Missing support for ProjectTo #11

Closed mc0re closed 5 years ago

mc0re commented 5 years ago

The following code:

var res = await mContext.Entities
    .ProjectTo<Model>(mMapper.ConfigurationProvider)
    .ToListAsync();

produces an error when executed with mocked db:

at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection`1 parameters, String paramName)
   at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable`1 parameters)
   at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, IEnumerable`1 parameters)
   at EntityFrameworkCoreMock.DbAsyncEnumerable`1.CompileExpression[T](Expression expression)
   at EntityFrameworkCoreMock.DbAsyncEnumerable`1..ctor(Expression expression)
   at EntityFrameworkCoreMock.DbAsyncQueryProvider`1.CreateQuery(Expression expression)
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](Object parameters, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ProjectionExpression.cs:line 62
   at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\Extensions.cs:line 76
   at MyCode.IRepository.GetAll()
   at MyTest.RepositoryShould.Add()
Result Message: 
Test method MyTest.RepositoryShould.Add threw exception: 
System.ArgumentException: Expression of type 'System.Linq.IQueryable`1[Model]' cannot be used for return type 'System.Collections.Generic.IEnumerable`1[Entity]'

ProjectTo is AutoMapper extension. Preprequisites: Entity class, Model class, and a map from Entity to Model.

huysentruitw commented 5 years ago

Seems like the ProjectTo expression expects an IEnumerable<Entity> where it gets a IQueryable<Model>. Currently, we don't tend to support extensions from 3rd party libraries, because of time constraints. If you could help and create a PR for this, I would be happy to review and add it into the library.

mc0re commented 5 years ago

I don't even know where to start in your code ☹️

On Mar 27, 2019 11:00, huysentruitw notifications@github.com wrote:

Seems like the ProjectTo expression expects an IEnumerable where it gets a IQueryable. Currently, we don't tend to support extensions from 3rd party libraries, because of time constraints. If you could help and create a PR for this, I would be happy to review and add it into the library.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/huysentruitw/entity-framework-core-mock/issues/11#issuecomment-477072418, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AIlo7t110UgsX5Do4FTOKnUimnT_0tiWks5va0ExgaJpZM4beo74.

huysentruitw commented 5 years ago

Can you add your unit-test or create a unit-test for this project that demonstrates the problem?

mc0re commented 5 years ago

Hi,

I have created a test case, but I can’t create a branch to push it to, so I don’t know how to create a pull request.

Mikhail


From: huysentruitw notifications@github.com Sent: Saturday, March 30, 2019 10:16:30 AM To: huysentruitw/entity-framework-core-mock Cc: mc0re; Author Subject: Re: [huysentruitw/entity-framework-core-mock] Missing support for ProjectTo (#11)

Can you add your unit-test or create a unit-test for this project that demonstrates the problem?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/huysentruitw/entity-framework-core-mock/issues/11#issuecomment-478224527, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AIlo7ow8Cm8Ho5whhCX2nUm5LE3OoYIdks5vbytugaJpZM4beo74.

huysentruitw commented 5 years ago

Hi Mikhail,

that's correct. You'll have to create your own fork of this repository and push your changes to your own fork.

To create a fork, go here: https://github.com/huysentruitw/entity-framework-core-mock and click the Fork button. After that, you can check out your fork and commit your branch to the fork.

When you're happy with the changes, you can create a pull-request from your branch to the master branch in this repository. If that doesn't work, just point me to your branch.

Regards, Wouter

mc0re commented 5 years ago

Okay, done 😊 The test case obviously fails.


From: huysentruitw notifications@github.com Sent: Sunday, March 31, 2019 3:47:32 PM To: huysentruitw/entity-framework-core-mock Cc: mc0re; Author Subject: Re: [huysentruitw/entity-framework-core-mock] Missing support for ProjectTo (#11)

Hi Mikhail,

that's correct. You'll have to create your own fork of this repository and push your changes to your own fork.

To create a fork, go here: https://github.com/huysentruitw/entity-framework-core-mock and click the Fork button. After that, you can check out your fork and commit your branch to the fork.

When you're happy with the changes, you can create a pull-request from your branch to the master branch in this repository. If that doesn't work, just point me to your branch.

Regards, Wouter

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/huysentruitw/entity-framework-core-mock/issues/11#issuecomment-478343494, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AIlo7mpaV2h6KveTRZfhOJLZNW_7BTvTks5vcLx0gaJpZM4beo74.

huysentruitw commented 5 years ago

Finally had some time to fix this. Please retry with version 1.0.0.24+. Feel free to reopen if you're still having trouble.

mc0re commented 5 years ago

This version requires .NET 2.2, it'll take some time before I switch the solution to that.

huysentruitw commented 5 years ago

You probably mean the version of EntityFramework ? Ok if I downgrade to 2.1.8?

huysentruitw commented 5 years ago

If so, please try version 1.0.0.25 or higher, where I've downgraded the EF Core dependency.

mc0re commented 5 years ago

Thanks, will do after Easter.