Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how can i unit test a entity framework class from a table?

Posted on 2014-10-01
5
Medium Priority
?
144 Views
Last Modified: 2014-11-05
how can i unit test a entity framework class from a table?
0
Comment
Question by:cbrune
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40356769
Please clarify your question.
0
 

Author Comment

by:cbrune
ID: 40356831
Want to mock ef return data
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40356844
Which mocking framework are you using?
0
 

Author Comment

by:cbrune
ID: 40356937
moq
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40357113
I haven't used EF extensively, and I certainly haven't mocked it yet, but as I understand you simply mock out the context, and then you can return what you need to.

e.g.

Mock<YourDataContext> mockContext = new Mock<YourDataContext>();

Open in new window


You would then define setups on the various "tables" in your context:

mockContext.SetupGet<IDbSet<YourTableName>>(ctx => ctx.PropertyToMock);

Open in new window


You can specify what should be returned on a call to that method if you need actual data:

Mock<YourDataContext> mockContext = new Mock<YourDataContext>();
Mock<IDbSet<YourTableName>> mockDbSet = new Mock<IDbSet<YourTableName>>();
List<YourTableName> fakeDatabaseObjects = new List<YourTableName>() { new YourTableName(), new YourTableName() };

mockDbSet.Setup(dbSet => dbSet.GetEnumerator())
            .Returns(fakeDatabaseObjects.GetEnumerator());

mockContext.SetupGet<IDbSet<YourTableName>>(ctx => ctx.PropertyToMock)
            .Returns(mockDbSet.Object);

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question