Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-01
5
Medium Priority
?
143 Views
Last Modified: 2014-11-05
how can i unit test a entity framework class from a table?
0
Comment
Question by:cbrune
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
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…

719 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