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't Mock class inherited from DBContext #67

Closed
drewfreyling opened this issue Dec 10, 2013 · 4 comments
Closed

Can't Mock class inherited from DBContext #67

drewfreyling opened this issue Dec 10, 2013 · 4 comments

Comments

@drewfreyling
Copy link

Given the following nuget packages:

<package id="EntityFramework" version="6.0.2-beta1" targetFramework="net45" />
<package id="Moq" version="4.1.1311.0615" targetFramework="net45" />

and code:

public class ClassA : DbContext
{
}

[TestMethod]
public void ShouldntThrowException()
{
     var mock = new Mock<ClassA>();
     var instance = mock.Object;
}

I'm getting an ArgumentException with "Type to mock must be an interface or an abstract or non-sealed class.". However I don't believe this is correct as neither ClassA or DbContext fit this criteria.

Am I missing something?

@rafaelfernandesnet
Copy link

​You should use Moq agains interfaces, right?

Maybe try to ​

public class ClassA : DbContext, IClassA
{
}

public class IClassA: IDbContext
{
}

[TestMethod]

public void ShouldntThrowException()
{
var mock = new Mock();
var instance = mock.Object;
}

On Tue, Dec 10, 2013 at 6:30 PM, Drew Freyling notifications@git.luolix.topwrote:

Given the following nuget package:

and code:

public class ClassA : DbContext{}
[TestMethod]public void ShouldntThrowException(){
var mock = new Mock();
var instance = mock.Object;}

I'm getting an ArgumentException with "Type to mock must be an interface
or an abstract or non-sealed class.". However I don't believe this is
correct as neither ClassA or DbContext fit this criteria.

Am I missing something?


Reply to this email directly or view it on GitHubhttps://github.com//issues/67
.

@drewfreyling
Copy link
Author

Well yes that exists as a workaround but from what I can tell it should also work against a non-sealed class.

@MikeGoldsmith
Copy link

Doesn't look like this is an issue since going from 6.0.2-beta1 to 6.0.2. Using @drewfreyling's code sample above, the mock.object returns correctly.

Newest package versions are:

<package id="EntityFramework" version="6.0.2" targetFramework="net45" />
<package id="Moq" version="4.2.1312.1622" targetFramework="net45" />

@kzu kzu closed this as completed Jan 28, 2014
@bricelam
Copy link

Sorry, we (the EF team) accidentally released 6.0.2-beta1 with internals visible to DynamicProxyGenAssembly2. This was fixed in 6.0.2. (See Work Item 1907.)

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

No branches or pull requests

5 participants