Wednesday, August 31, 2016

Unit Testing jQuery with QUnit

One of the main problems with writing JavaScript or jQuery code is that it is difficult to debug and test.

Well, that is where QUnit comes in!

QUnit allows Unit Testing your JavaScript/jQuery code through a standard web browser.

You can learn about QUnit here: https://qunitjs.com/

If you are working with older documentation for QUnit, you may encounter error messages regarding deprecated methods, in which case you should consult the upgrade guide: https://qunitjs.com/upgrade-guide-2.x/

To get started with a basic sample of a QUnit Unit Test, you can use the following sample code:


Implementing Remote Validation using Data Annotations in ASP.NET MVC

If you want to implement Remote Validation using Data Annotations on your ViewModels in ASP.NET MVC, you can follow this article: https://msdn.microsoft.com/en-us/library/gg508808(vs.98).aspx

Even though this article references ASP.NET MVC 3, it still applies to ASP.NET MVC 5.

In order to ensure that you do not encounter conflicts with the System.ComponentModel.DataAnnotations namespace while using the Remote attribute, you should decorate your ViewModels with the fully qualified namespace for the attribute in this manner:

[System.Web.Mvc.Remote("IsUID_Available", "Validation")]

Then, once you have the necessary appSettings configured in your Web.config (these are set by default in most ASP.NET MVC projects), you should be able to leverage your MVC Controller Validation code as follows:


This will then perform the validation via an AJAX call seamlessly within your ASP.NET MVC Web Application!

Tuesday, August 30, 2016

Server-side paging with ASP.NET Web API and Kendo UI Grids

If you need to implement a custom server-side paging solution with Kendo UI, it may not be readily obvious as to how to accomplish this!

Kendo UI has some code samples which partially addresses this scenario: https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/grid/custom-ajax-binding/KendoGridCustomAjaxBinding

However, this example is tightly coupled to Entity Framework and does not work so well with ASP.NET Web API...

So how exactly can you adapt the Controller code sample to your own needs that work with ASP.NET Web API?

First of all, you need to make sure your Kendo Grid is setup as follows:
Notice that you have to set ServerOperation(true)

Next, you have to set up your Controller code as follows:

That is all there is to it!

Monday, August 29, 2016

Unit Testing Data Annotation Validation/View Model Validation

If you are building functionality for validating your ViewModels either using IValidateableObject or through using Data Annotations/ValidationAttribute, you will probably want to be able to Unit Test this functionality as well!

Well, thankfully, this can be done quite easily by following this article: https://visualstudiomagazine.com/articles/2015/06/19/tdd-asp-net-mvc-part-4-unit-testing.aspx


Unit Testing ASP.NET MVC with Request.IsAjaxRequest()

One of the difficulties in Unit Testing ASP.NET MVC Web Applications is any dependence on HttpContext.

Code that you may encounter frequently in your ASP.NET MVC Controller methods is a check for whether or not the Request object is an AJAX Request.

So how exactly do you Unit Test this for your ASP.NET MVC Web Applications?

Well, there is an article on how to do just that!  http://thegrayzone.co.uk/blog/2010/03/mocking-request-isajaxrequest/

Once you set the Request Headers for the Mocked HttpRequestBase object, you will be able to return true for the Ajax request and complete your Unit Tests!


Sunday, August 28, 2016

Add xUnit.Net support to TeamCity

If you want to run your xUnit.Net Unit Tests to your TeamCity Builds, you can accomplish this quite conveniently using the xUnit.Net TeamCity plugin.

You can download the latest release of the xUnit.Net TeamCity plugin from here: https://github.com/carlpett/xUnit-TeamCity/releases/tag/1.1.3

Once you have installed the TeamCity plugin, you will be able to use xUnit.Net as a TeamCity Build step as follows:


That is all there is to it!!

Friday, August 26, 2016

Code Metrics PowerTool for Visual Studio 2015

If you want to run Code Metrics for your Visual Studio solution, you will need to download the appropriate version of Code Metrics Power Tool corresponding to your Visual Studio version.

For Visual Studio 2015, you can download the Code Metrics Power Tool from here: https://www.microsoft.com/en-us/download/details.aspx?id=48213

If you need to integrate the calculation of Code Metrics into your automated build process (such as Jetbrains TeamCity), then you can learn how to use Code Metrics from the command line here: http://blogs.msdn.com/b/camerons/archive/2011/01/28/code-metrics-from-the-command-line.aspx

If you need to make the XML results viewable, then you will probably need a set of XSLT and CSS files to transform the XML into HTML, then you will want to take a look at this article: https://blogs.msdn.microsoft.com/camerons/2011/02/20/code-metrics-reporting-and-xslt-debugging/

Unfortunately, the links to the XSLT and CSS are broken!

I was able to dig up a version of the MetricsResultsTransform.xslt file from another site which I am providing for your reference below:

However, I was not able to find the Default.css file, so this functionality is still missing from the XSLT transformation.

If you think Microsoft should ship a default set of XSLT and CSS files with the Code Metrics PowerTool, you can vote for this UserVoice item here: https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/15832576-ship-default-xslt-and-css-files-with-the-code-metr