Uncategorized

Unit Testing & Handling CRM Requests using Fakes C#

Recently we were working on Unit Testing using Fakes for our Plugin. We faced a strange issue while writing the Unit Test for code as we had request involved there.

We were using RetrieveAttributeResponse in our code. But we were unable to set the expected response in Unit Test as AttributeMetadata is a read-only attribute and we cannot set it using the standard way.

We found some alternative blogs which mentioned using Wrapper classes approach for the same:

http://www.alexanderdevelopment.net/post/2013/01/13/How-to-unit-test-C-Dynamics-CRM-interface-code-part-III

https://nishantrana.me/2014/01/25/unit-test-retrieveattributeresponse-in-crm-using-microsoft-fakes/

But I was not willing to change the code and use Wrapper classes for this. So, after investing my time in research I found that below approach and the code worked for us like a gem:

 

2018-07-05 19_05_58-CMHC.CRM - Microsoft Visual Studio (Administrator)

So as you can see here we just passed the ParameterCollection object to the response.Results property in the response with the parameter name as “AttributeMetadata” the property which I wanted to set along with the respective value.

And guess what it passed the corresponding value which I needed to set in the read-only property.

This approach can be used for any of the CRM request where the property is read-only.

Hope this helps.

1 thought on “Unit Testing & Handling CRM Requests using Fakes C#”

Leave a comment