Wednesday, September 05, 2012

Unit testing a ModelBinder

While moving some code from an old project I noticed that there were no unit tests around the model binders. Obviously they had been working but moving forward it would be nice to know that any changes were covered by tests and we'd know when they were broken.

Here's an example of the modelbinder:-

and this is the unit test for it:-

Tuesday, September 04, 2012

Was busy setting up a new QA environment today with one of the QA guys when he mentioned that we'd have to do exactly the same in a few days for another QA environment there were going to need.

Now I know there are loads of tools out there for quickly provisioning machines (Puppet, Chief as examples) but we just wanted to get a machine setup with the basics we needed after it had been built.

So as we went through solving all the problems I started to add each step to a .cmd file so we could easily re-run it later.

The majority of the work was checking for things like is SQL server installed, IIS setup, Message queues available etc....

So here's the "Pre req check" for it all really simple at the moment but a good starting point..

Couple of things:-
echo|set /p=... Running as ADMIN ... - causes an echo WITHOUT a CRLF
>nul 2>&1 - disables output from whatever command is being called (I knew about > nul but had never seen 2>&1 before which apparently redirects stderr see here for more info)

Thursday, July 26, 2012

Count the number of occurrences of number in range

Had a need to count the number of 1's in a list of numbers.

So if given the numbers 1,2,10,11,21 the count should be 5.

My first attempt (well it was actually my second but hey who's counting!) at a solution....
var ctr = 0;

Enumerable.Range(1, 20)
.ToList()
.ForEach(n => n.ToString().ToCharArray().ToList().ForEach(t => { if (t == '1') ctr++; }));

It works! But it's not very elegant.
Here's another solution that is less verbose and gives the same results.
var count = Enumerable.Range(1, 20)
.Select(i => i.ToString().ToCharArray())
.Sum(c => c.Count(ch => ch == '1'));

(Thanks to Matt for the second example :-))
Must remember to look at the available methods when trying to work these things out, SUM is obviously the way to go.

Getting at JSON data in unit tests

While writing some unit tests I needed to check that the JSON being returned was in the correct format and contained the expected data.

I had a quick google around to see what people were already doing and found and piece of code that would get the required property value. Here's some example code:-

Given a controller action like this:- and a unit test for it: -

I eventually moved the data.GetType()... code into a method so I could reuse it: - and then call it like this: - If I want to get at the rows....

The above examples make use of the Fluent assertion extensions.

Performance counters don't exist

Had a problem with performance counters not showing up on a dev box; with a simple 1 line command they were restored:-

LODCTR /R

Nice and simple for a change :-)