Solved

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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