Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to do Unit testing for MSCRM console application

Posted on by 475

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

  • Suggested answer
    JordiMontana Profile Picture
    JordiMontana 326 on at
    RE: How to do Unit testing for MSCRM console application

    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... :) )

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: How to do Unit testing for MSCRM console application

    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
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to do Unit testing for MSCRM console application

    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.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to do Unit testing for MSCRM console application

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to do Unit testing for MSCRM console application

    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
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How to do Unit testing for MSCRM console application

    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

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How to do Unit testing for MSCRM console application

    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-&amp;-Debugging

    Hope this helps.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans