By BoLOBOOLNE payday loans

Unit Tests… ugh!

Posted: March 24th, 2011 | Filed under: .NET, Libraries, Unit Tests
Author:
No Comments →

So I spoke a little about how I’ve become committed to changing the culture on my team. And I’m starting that with an effort in backfilling our app with unit tests. It’s a tremendously large task and to be honest I’m finding it really exhausting, annoying and I’m having a hard time seeing the benefits.

Then I realized that I’m just thinking about unit tests wrong, being too detailed and too broad in each test. This makes them both annoying and complicated to write as well as not really accomplishing their true goal. One of the challenges that I’m having is basically that there’s a big chunk of our application that’s nothing but forms over data. And that gives you, well, not much to test.

We have a three layered design for UI, Business Logic and Data Access. Common stuff. We user Service Location and inversion of control so each service class has a lot of details that goes with it. It’s a pretty small number of places that have detailed business logic. Most of our service layer methods (where I’m focusing my unit tests at the moment) are either pulling data together to display, or updating the data store.

I started off all of my unit tests with complicated checks and ordered assertions to make sure that things are happening in the correct order and beyond that… basically that the right calls are going to the data layer and the right data is being returned. Now I guess I’m coming to the conclusion that I was way over shooting what these need to do and making the tests way too complicated. Seriously, some of these tests were around 40-50 lines long. Preparing all of the data that was going to be passed around. Setting up all of the mocks and all of the expected calls etc. It’s tedious.
Read the rest of this entry »

Tags: , , , , ,

AutoMapper prefill

Posted: December 16th, 2010 | Filed under: .NET, Libraries, Programming
Author:
No Comments →

I wrote a series of posts about AutoMapper and where I found it useful several months ago. In some cases I think I was trying to fit it into something it wasn’t entirely intended for, which by the way is pretty much the comment I got from Jimmy Bogard the developer behind AutoMapper.

Anyway having integrated AutoMapper deeper into the application I work on I’ve found another great use. It’s a little strange perhaps, but it works out really well. I use it to handle prefills on extended objects. So if I have a base object that I extend and add a few fills I create a map for this with AutoMapper, ignoring the new fields. Then on construction I’m passing the base object and letting AutoMapper handle the prefill for me.

It’s not a complex idea but it’s simple and effective. I’ll give you a quick example of how we’re accomplishing that.

public class BaseClass { 
    public string MyProp { get; set; }
}

public class ExtendedClass : BaseClass {
    // a lot of my maps are created in a bootstrapper but for this situation
    // this works pretty well, though I admit it might be a little questionable architecturally
    static ExtendedClass() {
        Mapper.CreateMap<BaseClass, ExtendedClass>()
            .ForMember(dest => dest.MyNewProp, opt => opt.Ignore());
    }

    public ExtendedClass(BaseClass prefill) {
        Mapper.Map(prefill, this, typeof(BaseClass), typeof(ExtendedClass));
    }

   public string MyNewProp { get; set; }    
}

We’ve been doing this for a few months now and while we don’t need it frequently, when we do need it, it’s been great.

Tags: , , ,