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

JustMock And The Telerik ORM

3 Answers 31 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Veteran
Andrew asked on 06 Feb 2012, 03:08 PM
Just looking for some example on how to correctly Mock the ORM entities model.
any help would be great

Thanks

3 Answers, 1 is accepted

Sort by
0
Andrew
Top achievements
Rank 1
Veteran
answered on 06 Feb 2012, 08:40 PM
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
0
Accepted
Ricky
Telerik team
answered on 09 Feb 2012, 04:24 PM
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 >>
0
Andrew
Top achievements
Rank 1
Veteran
answered on 09 Feb 2012, 07:21 PM
thanks for your help
Tags
General Discussions
Asked by
Andrew
Top achievements
Rank 1
Veteran
Answers by
Andrew
Top achievements
Rank 1
Veteran
Ricky
Telerik team
Share this question
or