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

Support for expando entities #552

Closed maxkoshevoi closed 3 years ago

maxkoshevoi commented 3 years ago

Describe the bug I have a plugin the returns an entity record without logical name.

To Reproduce Please provide a unit test sample that would help speed up reproducing the error.

Something like this:

localContext.PluginExecutionContext.OutputParameters["Result"] = new Entity 
{
   ["Attribute1"] = "Value1"
};

This works fine in real CRM. Plugin response looks like this:

{
    "@odata.context": "https://schedulinggatest.crm10.dynamics.com/api/data/v9.0/$metadata#expando/$entity",
    "@odata.type": "#Microsoft.Dynamics.CRM.expando",
    "Attribute1": "Value1"
}

But FakeCRM fails to return such result. Execute() fails with:

System.ArgumentNullException: 'Value cannot be null.
Parameter name: key'

System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage)
    System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref System.Runtime.Remoting.Proxies.MessageData, int)
    Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(Microsoft.Xrm.Sdk.OrganizationRequest)
    Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(Microsoft.Xrm.Sdk.OrganizationRequest)
    Microsoft.Xrm.Client.Services.OrganizationService.Execute.AnonymousMethod__18(Microsoft.Xrm.Sdk.IOrganizationService)
    Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService<TResult>(System.Func<Microsoft.Xrm.Sdk.IOrganizationService, TResult>)
    Microsoft.Xrm.Client.Services.OrganizationService.Execute(Microsoft.Xrm.Sdk.OrganizationRequest)
    MyProject.IntegrationTests.Common.TestOrganizationService.Execute(Microsoft.Xrm.Sdk.OrganizationRequest)

Expected behavior I can return records without logical names from plugins

FakeXrmEasy and Dynamics 365 / CRM version FakeXrmEasy.9 v1.57.1 Dynamics 365 v9.2

Screenshots Not applicable

jordimontana82 commented 3 years ago

Hi Maksym,

Thanks for raising this. It looks like from the stack trace that you're attempting to run an integration test, not a unit test, is this raised in XrmRealContext as opposed to XrmFakedContext by any chance?

maxkoshevoi commented 3 years ago

@jordimontana82 It's an integration test since that plugin calls external API, and returns formatted result. So API is what being tested here, but plugin acts as an adapter, so it's also needs to be tested.

I'm note sure how to check what XrmContext is this, but no real CRM is called, if that's what you asking. Plugin is being executed by FakeCrm.

Some more information. If I change this line:

localContext.PluginExecutionContext.OutputParameters["Result"] = new Entity 
{
   ["Attribute1"] = "Value1"
};

To this:

localContext.PluginExecutionContext.OutputParameters["Result"] = new Entity("organization")
{
   ["Attribute1"] = "Value1"
};

Everything begins to work and Attribute1 is returned, but I cannot use it in production since real CRM returns empty record in this case (since organization entity doesn't have Attribute1 attribute)

Also, if I change it to this:

localContext.PluginExecutionContext.OutputParameters["Result"] = true;

Everything works in both real and fake crm.

jordimontana82 commented 3 years ago

Thanks by the stack trace it looks like it's using Xrm Client and therefore trying to hit an actual server, not the fake one.

Could you please post the integration test sample?

maxkoshevoi commented 3 years ago

You correct. Turns out our integration tests implement full WCF flow (and use FakeCrm as a data source), and this issue is within OrganizationService itself. Production code executes this plugin using WebApi, so everything works.

It seems like OrganizationService (WCF) just doesn't support records without logical names, while WebApi does.

jordimontana82 commented 3 years ago

I'll close this issue so as not related to FakeXrmEasy, since it is really in the SDK.