Solved

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

Posted on 2014-10-01
5
132 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 74

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 74

Expert Comment

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

Author Comment

by:cbrune
ID: 40356937
moq
0
 
LVL 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now