jordimontana82 / fake-xrm-easy

The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
https://dynamicsvalue.com/get-started/overview?source=git
Other
263 stars 182 forks source link

Calendar entity service.retrieve not returning the calendarrules attribute #528

Closed durgaprasadkatari closed 3 years ago

durgaprasadkatari commented 3 years ago

I'm facing an issue with service.retrieve call on the calendar entity. Calendar is special type entity in MS CRM when we do the retrieve call it will automatically return the "calendarrules" entity collection attribute, but if we specify the "calendarrules" in the columnset it is throwing the error "attribute doesn't exist in the calendar entity". To fix the unit test I have added the "calendarrules" attribute in the service.retrieve call but this is throwing the error in CRM. Please suggest a workaround to resolve this issue. var holidaySchedule = service.Retrieve("calendar", serviceCalendarId, new ColumnSet("calendarid", "calendarrules"));

@BetimBeja @jordimontana82

BetimBeja commented 3 years ago

Since the implementation of the IOrganizationService is based on Fakes you can Fake the result for your retrieve on this test, so that it returns the expected result:

  A.CallTo(() => service.Retrieve("calendar", A<Guid>._, A<ColumnSet>._)).Returns(new Entity("calendar") {Id=Guid.NewGuid() });
jordimontana82 commented 3 years ago

Hi @durgaprasadkatari thanks for raising this.

Just wondering if you tried an advanced find that produces the same as that .Retrieve but with a FetchXml query that returns the calendar rules? If that's the case then you could query it that way for the time being.

Otherwise, feel free to try what @BetimBeja suggested . I might look into this as an enhancement for the next version.

durgaprasadkatari commented 3 years ago

Hi @jordimontana82 Calendar entity is not available in advance find, but I tried in Fetchxml builder it returns the calendar rules even if I don't specify the attribute in the fetchxml query. I had converted the service.retrieve into RetrieveRequest in the plugin and mocked the RetrieveRequest in my unit test case using this below code and this was solved my problem.

`context.AddExecutionMock<RetrieveRequest>(request =>
        {
            var req = request as RetrieveRequest;
            if (req.Target.LogicalName == "calendar")
            {
                Entity calendarEntity = lstEntities[4];
                var response = new RetrieveResponse()
                {
                    Results = new ParameterCollection { { "Entity", calendarEntity } }
                };
                return response;
            }
            return new RetrieveResponse();
        });`

But I also tried the @BetimBeja approach now and it is also solved my problem. I changed my plugin code back to service.retrieve(). Thanks!

durgaprasadkatari commented 3 years ago

Do you want to close this issue?

jordimontana82 commented 3 years ago

Yes, I'm closing it and moving the enhancement to a separate issue #532