Search code examples
.nettddmoqrhino-mocks

How to test my business layer?


I have been using Moq and initially Rhino Mocks over the last year together with TDD. I have really enjoyed developing in this way however I have come to a point in my project where I feel maybe this is not what I need to be doing.

I have tested and drove out my design and all works fine. Now I have a layer above my objects where I need to test. Since I have a number of objects (using Inversion of Control via interfaces), stubbing and mocking all these services seems like so much work. In one test, I have to stub out at least 8 services before I can even test my code. It just seems like writing lots of code which has no real benefit and is so laborious.

My question is "Is there a better way to do this"? Is this where behaviour driven design or some other methodology would be better suited as I am not really doing unit testing?


Solution

  • If you need 8 services then you will need 8 stubs/mocks. I don't think you can avoid this.

    If you want to code less, then you may want to refactor your unit-test too and create a hierarchy or some other suitable OO commodity for group all the 8 services' setup code.

    EDIT

    Otherways, you could create a "megaobject". Let' suppose the 8 services implement each a different interface: IService1, IService2 and so on. In the unit test, you can create an interface that groups each service's interface:

    interface ISuperInterface: IService1, IService2, ... { }
    

    Then you just need to mock one object.