Constructor strategies for AutoFixture

For my unit tests I’m using an awesome tool Autofixture as data generator and as an Automocking container. But by default for creation of objects it uses the constructor with least number of arguments. Mark Seemann talks about this in his blog. Also he provides code on how to make Autofixture work the opposite way. But the code example in that blog post is outdated and no longer valid in the latest version of the framework.

So every time I need this (and this is not often) I have too search for the solution over-again. And just to save myself this research next time, I’ll put the solution here:

var fixture = new Fixture();

fixture.Customizations.Add(new MethodInvoker(new GreedyConstructorQuery()));

Test project per production project vs. Single project for all tests

For the last 7-8 months we have rolled a test project per production project. And it worked just fine when we started it. But over this weekend I have collapsed all test projects into one because many test project became a headache. And here are my reasons:

  1. The more projects you have in your Visual Studio solution, the slower VS is. I don’t know why that is, but for half-day I have tried excluding all extra projects and trying working with VS and it was much more snappier and happier. And we had 13 projects in our solution with total number of lines just above 133K (not including JS and HTML). And VS memory consumption have reduced in half when I went from 13 to 5 projects – that also made my PC happier.
  2. Package management on one test project takes 10 times less than on 6 projects. Every time you get new version of Moq, you need to update all your test projects. Or just one.
  3. Every time your production project gets a new dependency, you need to add that to your test projects. Or just one project.
  4. Did I tell you, dependency management gets 10 times easier with single test project?
  5. Less projects => less maintenance costs. And you don’t want to maintain test projects.

And what is the point of having a test project per project? Please somebody tell me! I have started that because I read that in books. Well, the books are wrong! There is no benefit of having test-project-per-project. Only headaches and slow Visual Studio.

Load jQuery Accordion from AJAX call

Today I needed to load a massive amount of text into dropped-down section on the page. And it was unlikely that users will expand that section, so no point in loading a huge load of data until it is needed. That is AJAX is for, isn’t it?

Here is the HTML for the Accordion:

<div class="accordion-ajax" data-url='/path/to/get/text/from'>
    <div class='accordion-head'>
       This is visible text to be presented for clicking
    </div>
    <div><!--This is required: it will be filled in with body of accordion when it is expanded--></div>
</div>

And here is the JavaScript code:

    $('.accordion-ajax').accordion({
        collapsible: true,
        active: false,
        clearStyle: true,
        autoHeight: false,
        header: '.accordion-head',          // point to the class that is used as a header
        heightStyle: 'content',
        icons: false,
        beforeActivate: function (event, ui) {
            var self = $(this); 

            // this bit is tricky. Make sure you have no empty space in the body of drop-down 
            if (ui.newPanel.html() == '') {         
                // taking data-url parameter from accordion header div
                // and load the returned contents into the accordion body.
                // presuming HTML is returned. 
                ui.newPanel.load(self.data('url'));
            }
        }
    }); 

See more details on API in jQuery Accordion API

Annoying Chrome New Tab page and how to turn it off.

23 Feb 2014, UPDATE: With the latest update in Chrome, this flag is disabled, so you’ll have to use the new tab page with search toolbar. I’ll try migrating to IE11. Chrome is dead now.

With the latest update of Chrome I’ve been presented with new “New Tab” page. It had massive Google logo with search bar (like one is not enough), on the right there was crap from Google+, notifications from G+ and some other shit. That was annoying. But the worst was that Google doddle was also brought through to my “New Tab” page. And today it had some sort of animation.

ANIMATION! in my browser, on an EMPTY page!!! ANIMATION!!!! I hate animation on on any web page, unless it is a video clip. And now I was presented with a fucking animation inside of the browser on an empty page. RAGE! Chrome is a new IE6. What a joke. Look at this: New_Tab

Anyway, you can disable that.

  1. Go to type chrome://flags in your address bar. flags_url

  2. You need to find setting Enable Instant Extended API Mac, Windows, Chrome OS. Change it from Default to disabled. flags_Disable

  3. To save the settings you’ll need to press “Relaunch Now” at the bottom of the screen. 2013-09-27 22_43_29-chrome___flags

And all should go back to normal. But people talk that this fix is quite likely to be removed in the future releases. Hopefully they’ll have enough negative user feedback, so they remove that crop from the New Tab page.

Assembly Version Mismatch and Bulk Package Update

In our solution we have 6 projects just for tests. Every test project has Autofixture, Moq and Autofixture with Automocking using Moq installed. The problem is Autofixture compiled against on Moq version 3. But Moq version 4 has been released long time ago.

So every time new Moq version comes out, I need to update Moq packages in all testing projects. You can do that through Nuget GUI in one go, but after that you need to update Assembly binding redirect.

Binding redirect allows your projects to have library A version 1.0 that depends on library B version 2.0. But actually your installation includes library B of version 3.0. Without binding redirect you would get a nasty runtime exception like this:

Continue reading

Error handling in MVC and nice error pages

It is vital for your application security not to show any internals when error happen. And you should be able to replace all internal error messages to nice user-friendly pages. It is a just nice for users – they are not getting splashes of oil, when engine is exploded, also another measure to improve site security.

There are lot of articles about error handling in ASP.Net MVC, but most of them do not cover the whole range. There is a very good resource on this, and I do recommend reading and understanding that first.

With error handling there are a lot of edge cases, and for every single one of them you need to provide a solution, otherwise your error messages will talk too loud about your implementation and that can lead to security vulnerability.

Continue reading

Elmah Shinangiens. Part 2.

In my last post I talked about ELMAH and how you can override connection string. But Atif Aziz (ELMAH author) pointed out that there is a better way to do that:


Fair enough, I thought. I did see that solution when originally needed to fix this problem. But I did not manage to quickly reproduce the code, so I went with a hacky way. This time I’ve spent a couple hours for that and did get what I wanted without dirty hacks.

Continue reading

ELMAH in Azure and Connection Strings business

ELMAH is a nifty tool that allows to you record exceptions that arise in your web-application. It is very handy and everyone should use it in addition to application logging. But if you are using Dependency Injection and have your connection strings provided by some service, then you might have trouble. For example, in our app, we use Azure Configuration settings and we have a service IConfiguration that has a lot of get-methods for different config settings in our app, including database connection string.

Continue reading

Test your constructors to be simple

In the previous blog post I wrote how it is important to have a simple constructor that only accepts dependencies. And violation of this rule cost me at least 12 hours of debugging and blocking issue for some developers in the team. Now I have a unit test to prevent this kind of problem in the future.

One of the suggestions in the comments was to use strict mocks and inject them into a controller. And don’t set up any kind of expectations on mocks. This way if dependencies are doing anything in the controller, they will fail the test.

Continue reading

Constructors should be simple

Whole day of yesterday I’ve spent debugging some “random” error that came up third time during this week. The issue did go away after some tribal dances around the code, but two other instances did not get resolved easier. The biggest problem was that only one of the developers on the team did get the issue, and everybody else were not affected. And the same happened in the new deployment. And the issue was quite major – the whole site did not load. Nothing beyond the login screen. And it was impossible to hook in the debugger, it was bypassing all the breakpoints.

I would’ve gone through the issue and try debugging it, by my machine was not affected by the problem. I could not reproduce the issue locally. But on my home machine the issue came up. Strange I through, but was glad it came up – means I can dig my teeth deep in the problem and debug it.

Continue reading