JustMock And The Telerik ORM

4 posts, 1 answers
  1. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 06 Feb 2012 Link to this post

    Just looking for some example on how to correctly Mock the ORM entities model.
    any help would be great

    Thanks
  2. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 06 Feb 2012 Link to this post

    here is my issue in more detail.
    I have the following method:
    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public static PlanningTeachingUnit GetByID(Guid id, Guid planningSandboxId)
    {
     
        PlanningTeachingUnit returnValue = CreateEmptyPlanningTeachingUnit();
     
        if (id != Guid.Empty && planningSandboxId != Guid.Empty)
        {
            WorkloadPlannerEntitiesModel workloadPlannerEM = new WorkloadPlannerEntitiesModel();
            WorkloadPlannerORM.Data.VPlanningTeachingUnitDetail ormPlanningTeachingUnit = workloadPlannerEM.VPlanningTeachingUnitDetails.Single(x => x.PlanningTeachingUnitID == id && x.PlanningSandboxID == planningSandboxId);
            returnValue = ConvertVPlanningTeachingUnitDetailToPlanningTeachingUnit(ormPlanningTeachingUnit);
        }
        return returnValue;
    }

    I want to mock the WorkloadPlannerEntitiesModel in my test so when I call this method it will use my mock instead of the ORM.

    this is what I have tried:

    [TestMethod]
    public void Test()
    {
     
        Guid Id = new Guid("F87B7E90-F920-43FA-9A64-029AF82D3A8E");
        Guid planningSandboxId = new Guid("8AEEBED8-6128-4F57-91C8-0956B046C149");
        int planningCourseSectionId = 1234;
       
        WorkloadPlannerEntitiesModel workloadPlannerEntitiesModel = Mock.Create<WorkloadPlannerEntitiesModel>(Constructor.Mocked);
     
        Mock.Arrange(() => workloadPlannerEntitiesModel.VPlanningTeachingUnitDetails.ToList()).Returns(GetPlanningTeachingUnits());
        Mock.Arrange(() => workloadPlannerEntitiesModel.VPlanningTeachingUnitDetails.Single()).Returns(GetPlanningTeachingUnit(new Guid("F87B7E90-F920-43FA-9A64-029AF82D3A8E"), planningSandboxId, planningCourseSectionId));
     
        PlanningTeachingUnit planningTeachingUnit = WorkloadPlannerORM.Facade.WorkloadPlannerFacade.GetByID(Id, planningSandboxId);
     
        Assert.AreEqual(Id, planningTeachingUnit.ID);
        Assert.AreEqual(planningSandboxId, planningTeachingUnit.PlanningSandboxID);
     
    }

    but when it trys to instantiate WorkloadPlannerEntitiesModel GetByID method it fails on a conectionstring error
  3. DevCraft R3 2016 release webinar banner
  4. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 09 Feb 2012 Link to this post

    Hi Andrew,

    Thanks for reporting the issue. However it looks to me that you are expecting the following line to take the instance from the mock which you have defined using Constructor.Mocked in your test method:
    if (id != Guid.Empty && planningSandboxId != Guid.Empty)
        {
            WorkloadPlannerEntitiesModel workloadPlannerEM = new WorkloadPlannerEntitiesModel();

    But Mock.Create actually just returned you the fake instance which you can setup in ways you want it to act, not like that it will replace all consecutive calls with fake constructor. The other way to get around this is to define a connection string settings and then mock the expected methods using IgnoreInstance switch or fake the following line itself:

    PlanningTeachingUnit planningTeachingUnit = WorkloadPlannerORM.Facade.WorkloadPlannerFacade.GetByID(Id, planningSandboxId);

    But it’s a great point therefore we can always include:
     
    Mock.ReplaceAll(() => new WorkLoadEntitiesModel()).With(target);

    I am adding it as a feature request which you can further follow here:
    http://www.telerik.com/support/pits.aspx#/public/justmock/9686

    In addition, in your test method instead of mocking ToList() and Single() separately you can use the ReturnsColleciton() switch that will replace the whole collection with fake one, which you can use in a loop or call Single() , First(), etc on it.


    Kind Regards,
    Mehfuz
    the Telerik team

     

    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 09 Feb 2012 Link to this post

    thanks for your help
Back to Top