Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Trying to create ExecuteMultipleResponse in unit test but Responses is null

Posted on by 70

I am creating a unit test for some older code and need to instantiate and return the ExecuteMultipleResponse object. While I'm able to get the object created, the internal Responses is null and I can't return any failure info for the request. How can I properly create ExecuteMultipleResponse?

Categories:
  • CU02060244-0 Profile Picture
    CU02060244-0 2 on at
    Trying to create ExecuteMultipleResponse in unit test but Responses is null
    To create an ExecuteMultipleResponse in a unit test, you need to initialize and assign a value to the Responses property. Here are a few ways you can do this:
    Create a list of responses with rice purity test (List) and assign it to the Responses property of the ExecuteMultipleResponse.
    Create an array of ExecuteResponse objects and assign it to the Responses property.
    Initialize the Responses with one or more ExecuteResponse instances.
    Example:

    var responses = new List<ExecuteResponse>();
    // Add the ExecuteResponse instances to the responses list
    var executeMultipleResponse = new ExecuteMultipleResponse
    {
        Responses = responses
    };
    By doing this, you can then inspect and handle any error information within the ExecuteMultipleResponse. 
  • Verified answer
    TrevorK Profile Picture
    TrevorK 70 on at
    RE: Trying to create ExecuteMultipleResponse in unit test but Responses is null

    figured this out and updating to share the solution. The Responses property is just looking at Results for the key "Responses".

    From Microsoft.Xrm.Sdk.Messages -

                   if (!base.Results.Contains("Responses"))

                   {

                       return null;

                   }

                   return (ExecuteMultipleResponseItemCollection)base.Results["Responses"]

    So to make this work, you only need to create the response collection (ExecuteMultipleResponseItemCollection) and then add it to the Results with the key "Responses"

     ExecuteMultipleResponse response = new ExecuteMultipleResponse();

     ExecuteMultipleResponseItemCollection responseItemCollection =

       new ExecuteMultipleResponseItemCollection();

     response.Results.Add("Responses", responseItemCollection);

  • TrevorK Profile Picture
    TrevorK 70 on at
    RE: Trying to create ExecuteMultipleResponse in unit test but Responses is null

    Thanks Scott. I'm using VS Fakes and I'm trying to do this from my ExecuteOrganizationRequest method within the fake. I check if the request is an ExecuteMultipleRequest and then look at the contents of the requests in the Requests collection. Now I'm trying to create the Responses collection but that doesn't have a setter. I tried to just add to the Responses collection (Responses.Add()) but since the collection is null, that doesn't work. Seems when I call new on ExecuteMultipleResponse, it doesn't create the full object.

    Below is the concept. Here I was calling clear instead (just in case I was adding the wrong type and causing an issue that way). Since Responses is null, still won't work.

                   if (r is ExecuteMultipleRequest)

                   {

                       ExecuteMultipleResponse response = new ExecuteMultipleResponse();

                       response.Responses.Clear();

  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 50,166 on at
    RE: Trying to create ExecuteMultipleResponse in unit test but Responses is null

    You will need to use a mocking framework such as Rhino Mocks or Fakes in Visual studio.

    See www.develop1.net/.../Unit-Testing-Dynamics-CRM2011-Pipeline-Plugins-using-Rhino-Mocks.aspx

    Hope this helps,

    Scott

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans