This is a migrated thread and some comments may be shown as answers.

Howto? Entity Framework with DBContext

6 Answers 200 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dirk
Top achievements
Rank 1
Dirk asked on 24 Aug 2012, 07:53 AM
Hello,
do you have any simple examples how to use JustMock with Entity Framework and DBContext?

Regards
Dirk

6 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 27 Aug 2012, 08:59 AM
Hi Dirk,

Thank you for contacting Telerik support center.

You can find examples about getting started with Telerik`s JustMock in our online help documentation:

·         http://www.telerik.com/help/justmock/introduction.html

There is an exact link, where you can find examples about mocking entity framework objects.

·         http://www.telerik.com/help/justmock/advanced-usage-entity-framework-mocking.html

Please let us know if these examples work for you or you need something else.

Another source, where you can find your answers is the Telerik`s JustMock community forum. There are some articles, you may find helpful.

(e.g. http://www.telerik.com/community/forums/justmock/general-discussions/new-to-mocking-working-with-entity-framework.aspx )

I hope this information resolves your case. 

Regards,
Kaloyan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Dirk
Top achievements
Rank 1
answered on 27 Aug 2012, 09:09 AM
Hi Kaloyan ,

thank you for your answer.
Your examples in your online help seeems to be outdated.
Entity Framework 4.1 introduced a new technology (called DBContext) to access the database.

Your examples still use old ObjectContext methods.
Do you plan to update your examples?

Regards
Dirk
0
Ricky
Telerik team
answered on 28 Aug 2012, 05:42 AM
Hi Drik,
Thanks again for contacting us. I have included a sample project contaning example of faking colleciton from DbContext:
[Test]
public void ShouldReturnFakeCollectionWhenExpected()
{
    NerdDinners nerdDinners = new NerdDinners();
 
    Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());
 
    var query = from d in nerdDinners.Dinners
                where d.DinnerID == 1
                select d;
 
    Assert.AreEqual(1, query.Count());
    Assert.AreEqual(1, query.First().DinnerID);
 
}

Also, shows an example of mocking the process of saving an entity.

[Test]
       public void ShouldFakeAddingNewEntityToContext()
       {
           NerdDinners nerdDinners = new NerdDinners();
            
           var dinner = new Dinner { DinnerID = 1 };
 
           IList<Dinner> dinners = new List<Dinner>();
 
           Mock.Arrange(() => nerdDinners.Dinners.Add(dinner)).DoInstead((Dinner d) => dinners.Add(d));
           Mock.Arrange(() => nerdDinners.SaveChanges()).DoNothing();
 
           nerdDinners.Dinners.Add(dinner);
           nerdDinners.SaveChanges();
 
           Assert.AreEqual(1, dinners.Count);
           Assert.AreEqual(1, dinners[0].DinnerID);
       }

Please check them out and let us know if you still have questions.

Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Neil
Top achievements
Rank 1
answered on 17 Dec 2012, 11:14 PM
Hi,

I opened your project in Visual Studio 2012 and updated the Telerik.JustMock reference to v2012.3.1016.3. After running the tests included in the project, ShouldFakeAddingNewEntityToContext fails, while ShouldReturnFakeCollectionWhenExpected passes.

Looking at the error, something is going on when nerdDinner.SaveChanges() is called.

Test Name:  ShouldFakeAddingNewEntityToContext
Test FullName:  EntityFramework_Test.EntityFrameworkTest.ShouldFakeAddingNewEntityToContext
Test Source:    EntityFramework\EntityFrameworkTest.cs : line 31
Test Outcome:   Failed
Test Duration:  0:00:00.0436006
 
Result Message:
Test method EntityFramework_Test.EntityFrameworkTest.ShouldFakeAddingNewEntityToContext threw exception:
System.IO.FileLoadException: Could not load file or assembly 'Telerik.JustMock_9acc98380f6e4eccaed32b689b25f750, Version=2012.3.1016.3, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
Result StackTrace: 
at System.Data.Entity.DbContext.SaveChanges()
   at EntityFramework_Test.EntityFrameworkTest.ShouldFakeAddingNewEntityToContext() in EntityFramework\EntityFrameworkTest.cs:line 42

I have tested this in Visual Studio 2010 with the updated Telerik.JustMock reference, and all tests pass.

Any thoughts or ideas would be appreciated. Thanks in advance.
0
Neil
Top achievements
Rank 1
answered on 17 Dec 2012, 11:25 PM
I forgot to mention that I updated the project to use the Visual Studio Unit Testing Framework, rather than NUnit, to more closely resemble the current project I'm working on.

Regards,
Neil
0
Kaloyan
Telerik team
answered on 18 Dec 2012, 09:58 AM
Hi Neil,

Thank you for reporting this issue.

We reproduced the matter you are experiencing on our side and it seems that it`s due to a known bug in the Q3 release of JustMock. However, this issue has been already fixed and a release of the SP1 is coming this week

In the new version, you will also find a lot of other bug fixes and support for NCover v4.0 workstation, dotTrace and other profiler using tools.

I hope this information helps.
 
Kind regards,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
Dirk
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Dirk
Top achievements
Rank 1
Ricky
Telerik team
Neil
Top achievements
Rank 1
Share this question
or