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)

How to do Unit testing for MSCRM console application

(0) ShareShare
ReportReport
Posted on by 483

Hello,

I have developed one batch job in console application  to update Case entity and now i want to implement Unit testing for this in VS Unit test. Please suggest how to implement this with any sample ? what are references need to add ? 

Thanks,

Krishna

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi krishna,

    Below are a couple of links to posts related to unit testing with Dynamics CRM. In general any one of these or the combination of these should get you started. There is no one right answer on how to do this, but this is a good starting place:

    community.dynamics.com/.../unit-testing-within-microsoft-dynamics-crm

    alexanderdevelopment.net/.../how-to-unit-test-c-sharp-dynamics-crm-interfaces-code

    github.com/.../Unit-Testing-&-Debugging

    Hope this helps.

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Prasad,

    We had requirment of updating old data on some condition so we created console app and in that we inert log in the text file, so we could check the step that are being processed.

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    on at

    I personally like the best Fak-xrm-easy framework from @jordimontana:

    github.com/.../fake-xrm-easy

    Altought it is dedicated to unit test workflows there is access to faked IOrganizationService that can be also used for unit testing console applications that reference to Dynamics CRM.

    Cheers!

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    I would suggest to write a text file log and make sure you will write log in every case for success and also for error as well. In addition debug the code for one dummy data.  It will be easy and will take less time I think.

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Krishna ,

    I would suggest to write a text file log and make sure you will write log in every case for success and also for error as well. In addition debug the code for one dummy data.  It will be easy and will take less time I think.

    Hope this helps.

  • dkrishna Profile Picture
    483 on at

    I have created Log file but i want to do UnitTest in Visual Studio which we do normally for plugins using Microsoft.XRM.sdk.8.0.0.0.Fakes . i want to know how to implement same for Console app.

  • Suggested answer
    JordiMontana Profile Picture
    326 on at

    Hi Krishna,

    Just replying to your tweet sorry and FakeXrmEasy.

    Did you have a look at the get started (https://dynamicsvalue.com/get-started/overview) page? It contains basic information about how to get started.

    Now, back to your specific question. FakeXrmEasy is not to be meant to be used just for codeactivities or plugins, it essentially exposes an implementation of IOrganizationService with a query engine and other CRM messages already implemented. So basically you can unit test any .net code using that is connected to Dynamics 365 CE (CRM).

    First, you'll need to setup your console app so it only references the interface (IOrganizationService), maybe refactor its logic into a separate class, so it will be easier to unit test. Once you have that...

    The way I would unit test that is by something similar to this (in a unit test project with a reference to your console app):

    [Fact]

    public void Should_update_a_case_property_when_some_class_method_is_called()

    {

    /*****   ARRANGE PHASE HERE ****/

    var ctx = new XrmFakedContext();

    var case = new Case()

      { Id = Guid.NewGuid(), 

        //Any other attributes here...

      };

    //defines an initial state where we only have one case record

    ctx.Initialize(case);

    /*****   ACT PHASE HERE ****/

    //returns an instance of an organization service already mocked, any future calls to that service will create / delete, / update entities in the faked context (ctx)

    var service = ctx.GetOrganizationService();  

    //Say your class in the console app was called...  SomeClass.cs with a constructor to pass in the organization service like public //SomeClass(IOrganizationService)

    var someClass = new SomeClass(service);  //your class will use the fake service from a unit test, a real service when invoked from the console app

    someClass.UpdateCase(case);

    /*****   ASSERT PHASE HERE (we query the context to check if an attribute(s) was updated to some value(s) )****/

    var updatedCase = ctx.CreateQuery<Case>().FirstOrDefault();

    Assert.Equal("someValue", updatedCase.SomeProperty);

    }

    As you can see, with FakeXrmEasy I didn't have to mock anything...

    That's it, hope it makes sense (was typing directly into the text editor so there might be errors... :) )

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