Over the years, this blog has been about many different things related to software development with Microsoft technologies. Since my current role focuses on PowerApps & Flow, that's what it's primarily about right now.
UPDATE: Fixed the video resolution issue. Higher resolution (720p) version is available.
After writing my post on unit testing plugins, I’ve been asked a few times about how to do the same for Dynamics CRM 2011 JavaScript web resources. While I knew it could be done, truth be told, I had never done any significant JavaScript unit test using Visual Studio other than unit testing Script# code using MSTest (the default unit testing framework in Visual Studio). Sure, I had played around with QUnit. However, QUnit is one of those JavaScript testing frameworks which requires tests to run within a browser. What I wanted was an approach which met the following criteria:
Integrate with MSTest so:
I have one test dashboard for both my plugin & JavaScript unit tests
I get as much of the TFS integration goodness I can get
Gated check-ins
Running tests on a build server
Etc.
Write and debug my code without having to constantly deploy to a CRM server to test and iterate on my code
Mock things like Xrm.Page, web service calls, interaction with the HTML DOM, etc.
So I scoured the internet, tried a bunch of things I came across, and finally landed on approach which combines lessons learned from the following resources:
While the catalyst for this video was to demonstrate JavaScript unit testing in the context of Dynamics CRM 2011, the approach can be applied to any scenario where you are using JavaScript and some additional API (SharePoint, standard web app, etc.).
Just like with my post on unit testing plugins, it is not my goal here to convince you that you should embrace unit testing. It’s for those of you who have already bought into the value of unit testing, want to apply it to JavaScript web resource development, and do it in a way that meets my stated criteria above. In the video, I assume you understand concepts like mocks, fakes, etc.
You can grab the finished example I show in the walkthrough here:
This is probably the most promised, but never published blog post on my list of potential posts. I’ve managed to convince enough CRM developers I’ve run into that they should get some test driven development with unit testing religion. Especially because of the added productivity benefits for CRM plugin development. This post isn’t to convince you of the benefits of unit testing. There are plenty of articles/blogs which cover that topic just fine. I’m not interested in debating the virtues of unit testing. Love it or leave it;).
The purpose of this post/video is to show you how I develop and unit test plugins. There are a few articles out there about how to unit test CRM 2011 plugins. I haven’t been satisfied with the approaches I’ve seen. I landed on an approach which combines using the CRM 2011 Plugin Testing Tools for mocking IServiceProvider and Moles fore detouring types used inside the plugin which are difficult or time consuming to mock such as OrganizationServiceContext, Web Service calls, and others.
The series above focuses on using Fakes in VS2012. Fakes is the successor to Moles. However, the general techniques still apply whether using Fakes/Moles. If you are using VS2012, then use Fakes. If you are using VS2010, then use Moles.
I find many people who are new to unit testing have questions about where it fits in relation to other forms of testing. Unit testing does not replace other forms of testing such as integration testing (i.e. testing the plugin running in CRM). TDD through unit testing is an upfront developer discipline. It requires a certain culture and mindset on a dev team. In my opinion and experience, it helps me produce better code and allows me to catch more (not all) bugs early on, before handing things off to other testers. It also makes the others testers lives/jobs easier.
When applying this approach to CRM plugins, there’s a bonus side effect that (for me) allows me to be more productive in authoring my plugin code because I can:
Iterate (make code changes, build, debug and/or test, repeat) rapidly without needing to deploy to a CRM server
Iterate on my code rapidly without affecting the external systems
Use code coverage to identify untested scenarios which enable me to build better, more complete tests.
This, in turn, allows me to build better, less buggy code.
The nature of having a suite of automated tests allows you to identify issues and regressions in your code early on as you make changes
This becomes more and more valuable over time as you build out your plugins and corresponding tests
Other devs don’t have to know how to test your code. They just run the tests you’ve written
As part of building my WP7 app, I spent a fair amount of time trying to get code coverage working for my unit tests. I tried a number of different approaches including using the Silverlight Unit Test Framework and NUnit + dotCover. I finally landed on an approach that worked for me using the unit testing capabilities in Visual Studio 2010. Since this is a much confused topic with very little info on the web on how to successfully get working for Silverlight/Windows Phone apps, I published a screencast on the subject. I hope you find it useful.
In this screencast, you will learn how to get code coverage for your Silverlight and/or Windows Phone ViewModels through unit testing. You can learn more about code coverage in Visual Studio 2010 from: