Can no longer mock DbContext in EF 6.3 Preview.
See original GitHub issuePreviously 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
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (11 by maintainers)
Top Results From Across the Web
Mocking DBContext in EF 6 not working as expected
The problem is myDbSet.GetEnumerator().Returns(myData1.GetEnumerator());. Which will return the same enumerator instance every time it is ...
Read more >Announcing Entity Framework Core 3.0 Preview 7 and ...
Today, we are making new previews of EF Core 3.0 and EF 6.3 ... For example, the following query will no longer work...
Read more >How to Mock EF Core DbContext
We are going to learn how to mock an EF Core DbContext by using two popular libraries: Moq. EntityFrameworkCore.
Read more >Repository pattern or DbContext - what's your poison?
Trying to mock DbSet<T> or IQueryable<T> just wont work unless you fall back to the sync methods, whereas you lose the benefits of...
Read more >Entity Framework 6.3 and .NET Core 3 Support
NET Core 3.0 Preview 9, and despite its preview status, Microsoft officially recommends it to full production use. Besides, Microsoft sends some ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@davidroth Looks like it just missed preview 8. Should be in the daily builds and preview 9.
@Pinski Correct.