Solved

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

Posted on 2014-10-01
5
140 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 500 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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

762 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