web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Mocking RetrieveEntityResponse - Is it possible?

(1) ShareShare
ReportReport
Posted on by 265

This excellent article:

alexanderdevelopment.net/.../how-to-unit-test-c-dynamics-crm-interface-code-part-iii

demonstrates how it is possible to mock a RetrieveAttributeResponse to enable Unit Testing of plugin code which uses this Response class. It creates a wrapper class which can be used in the Unit Test.


I've been using this method to mock not only the RetrieveAttributeResponse, but also the RetrieveMultipleResponse and RetrieveEntityResponse classes.

However, I've hit a problem when trying to work with my RetrieveEntityResponseWrapper. The response needs to contain an instance of EntityMetadata, which I can instantiate in my mocked response. However, I need to set the Attributes property of the EntityMetadata, which is a read-only property.
So in some way I need to have a wrapped class within a wrapped class, and I don't see how I can do this?

Is what I'm trying to do possible - can I mock the read-only Attributes property of the EntityMetadata property of my RetrieveEntityResponseWrapper?

Many thanks for your thoughts,
Ben

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    How about using Fakes?

    This test would pass - showing how to return AttributeMetadata from the read only Attributes property:

    var entityMetadata = new EntityMetadata();
    using (ShimsContext.Create())
    {
        AttributeMetadata[] attributes = new AttributeMetadata[1];
        attributes[0] = new AttributeMetadata();
        Microsoft.Xrm.Sdk.Metadata.Fakes.ShimEntityMetadata.
            AllInstances.AttributesGet = (a) => { return attributes; };
        Assert.AreEqual(1, entityMetadata.Attributes.Length);
    }
    


    Hope this helps!

  • Verified answer
    JordiMontana Profile Picture
    326 on at

    Hi Ben,

    Sure you can. One simple solution I´ve come up with is by using Reflection. Here's what FakeXrmEasy is doing behind the scenes to automatically mock Entity Metadata responses for instance.

    github.com/.../RetrieveEntityRequestExecutor.cs

  • Ben Walker Profile Picture
    265 on at

    You guys are awesome. As you know I'm already using FakeXRMEasy so I'll look at Jordi's suggestion first. Many thanks as ever for your most excellent contributions.

  • Suggested answer
    Ben Walker Profile Picture
    265 on at

    Working perfectly after a few issues with early bound generation. Thanks so much.

  • JordiMontana Profile Picture
    326 on at

    Glad it is working :)

  • Riteesh Profile Picture
    on at

    Hey Jordi, I have ascenario where the requestentityrequest has entityfilters set to entityfilters.relationships. Here is the code snippet for reference:

    RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest

                       {

                           EntityFilters = EntityFilters.Relationships,

                           LogicalName = primaryEntity

                       };

    FakeXRMEasy is throwing this error message:

    "At least EntityFilters.Entity or EntityFilters.Attributes must be present on EntityFilters of Request."

    Is there any workaround for this?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans