Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can no longer mock DbContext in EF 6.3 Preview. #1020

Closed
Pinski opened this issue Jul 9, 2019 · 13 comments · Fixed by #1105
Closed

Can no longer mock DbContext in EF 6.3 Preview. #1020

Pinski opened this issue Jul 9, 2019 · 13 comments · Fixed by #1105

Comments

@Pinski
Copy link

Pinski commented Jul 9, 2019

Previously I was able to mock my DbContext for testing purposes using Moq like so:

var mock = new Mock<MyDbContext>();
var service = new MyService(mock.Object);

Ever since upgrading to EF 6.3 Preview 6 now this code throws the following exception

System.ArgumentException
  HResult=0x80070057
  Message=Type to mock must be an interface or an abstract or non-sealed class. 
  Source=Moq
  StackTrace:
   at Moq.CastleProxyFactory.CreateProxy(Type mockType, IInterceptor interceptor, Type[] interfaces, Object[] arguments)
   at Moq.Mock`1.InitializeInstance()
   at Moq.Mock`1.OnGetObject()
   at Moq.Mock.get_Object()
   at Moq.Mock`1.get_Object()
   at Ancestry.Rosetta.UnitTests.Services.CultureServiceTests.InitializeService() in C:\Projects\rosetta-core\Ancestry.Rosetta.UnitTests\Services\TestsOf_CultureService.cs:line 95

Inner Exception 1:
TypeLoadException: Method 'InitializeLazyInternalContext' on type 'Castle.Proxies.RosettaEntitiesProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is overriding a method that is not visible from that assembly.

I found an old question on StackOverflow where a similar error was occurring during the upgrade from EF 4.2 to EF 4.3. If I understand the post correctly, InternalsVisibleTo attributes were not being properly stripped out causing Moq to fail:
https://stackoverflow.com/a/9266279/311444

Could something similar to this be happening in EF 6.3?

Steps to reproduce

Trying to instantiate an Object of a mocked DbContext throws an exception.

var mock = new Mock<MyDbContext>();
var service = new MyService(mock.Object);

Further technical details

EF version: 6.3.0-preview6-19304-03
Moq version: 4.12.0
Operating system: Windows 10
IDE: Visual Studio 2017 15.9.13 and Visual Studio 2019 16.2.0 Preview 3.0

@bricelam bricelam self-assigned this Jul 19, 2019
@bricelam bricelam added this to the 6.3.0 milestone Jul 19, 2019
@MrStevenHill
Copy link

Also experiencing this issue

@bricelam
Copy link
Contributor

Looks like 2,234 (74 functional; 16%) of our tests fail without [InternalsVisibleTo("DynamicProxyGenAssembly2")]

@ajcvickers We should discuss what we want to do here. This was regressed when we moved the build to Arcade.

@ajcvickers
Copy link
Contributor

@bricelam So am I correct in saying is that Arcade doesn't have a mechanism to strip InternalsVisibleTo from the build, but:

  • If we always generate builds without InternalsVisibleTo, then our tests fail
  • If we always generate a build with InternalsVisibleTo, then it's a breaking change to our customers

Is that correct?

@bricelam
Copy link
Contributor

Correct, we could...

  • Skip the failing tests
  • Customize our build to test against a version with InternalsVisibleTo but produce a package using a version without it (build twice)
  • Ship with the regression

@ajcvickers
Copy link
Contributor

@bricelam How feasible is the second option?

@bricelam
Copy link
Contributor

It should be as easy as creating a second csproj--referencing one in the tests and the other in the NuGet package.

@bricelam
Copy link
Contributor

There may also be other ways to re-use the same project and re-build it after running the tests, but two projects would be the simplest solution

@ajcvickers
Copy link
Contributor

Okay, simple is good. Let's go with that.

@divega
Copy link
Contributor

divega commented Jul 23, 2019

@bricelam in case you haven’t seen it, this PR contains some relevant details, including an “unofficial feature” to generate InternalsVisibleTo in Arcade: dotnet/sdk#3439.

@Pinski
Copy link
Author

Pinski commented Jul 24, 2019

Thanks everyone for looking into this. Just to confirm, I'm assuming a fix for this was NOT included in the incremental Entity Framework 6.3 Preview 7 that was released yesterday.

@ajcvickers
Copy link
Contributor

@Pinski Correct.

bricelam added a commit to bricelam/ef6 that referenced this issue Jul 30, 2019
bricelam added a commit to bricelam/ef6 that referenced this issue Jul 31, 2019
bricelam added a commit to bricelam/ef6 that referenced this issue Jul 31, 2019
@davidroth
Copy link
Contributor

Just had the same issue testing 6.3 Preview 8, unfortunately the fix is not in there. Will there be a Preview 9 @ajcvickers ?

@ajcvickers
Copy link
Contributor

@davidroth Looks like it just missed preview 8. Should be in the daily builds and preview 9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants