Howto? Entity Framework with DBContext

7 posts, 0 answers
  1. Dirk
    Dirk avatar
    43 posts
    Member since:
    May 2011

    Posted 24 Aug 2012 Link to this post

    Hello,
    do you have any simple examples how to use JustMock with Entity Framework and DBContext?

    Regards
    Dirk

  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 27 Aug 2012 Link to this post

    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.

  3. DevCraft R3 2016 release webinar banner
  4. Dirk
    Dirk avatar
    43 posts
    Member since:
    May 2011

    Posted 27 Aug 2012 Link to this post

    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
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 28 Aug 2012 Link to this post

    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.

  6. Neil
    Neil avatar
    2 posts
    Member since:
    Sep 2012

    Posted 17 Dec 2012 Link to this post

    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.
  7. Neil
    Neil avatar
    2 posts
    Member since:
    Sep 2012

    Posted 17 Dec 2012 Link to this post

    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
  8. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 18 Dec 2012 Link to this post

    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.
Back to Top
DevCraft R3 2016 release webinar banner