Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

MbUnit fixtures, or "How to make your tests pass through attribute attrition"

using MbUnit.Framework;
using Rhino.Mocks;
namespace Suvius.Applications.LoveDoctor.Tests
{
    public class CritterFixationFixture
    {
        private ISpeciesSelectorPresenterView view;
        private MockRepository mocks;
        private SpeciesSelectorPresenter presenter;

        [ SetUp ]
        public void SetUp( )
        {
            mocks = new MockRepository( );
            view = mocks.DynamicMock<ISpeciesSelectorPresenterView>( );
            presenter = new SpeciesSelectorPresenter( view );
        }

        [ Test ]
        public void Should_initialize_view_with_list_of_projects( )
        {
            using ( mocks.Record( ) )
            {
                view.LoadCritters( );
            }
            using ( mocks.Playback( ) )
            {
                presenter.InitializeView( );
            }
        }
    }

    public class SpeciesSelectorPresenter
    {
        private readonly ISpeciesSelectorPresenterView _view;

        public SpeciesSelectorPresenter( ISpeciesSelectorPresenterView view )
        {
            _view = view;
        }

        public void InitializeView()
        {
        }
    }

    public interface ISpeciesSelectorPresenterView
    {
        void LoadCritters( );
    }
}

How’s THAT for throwing you into the fray? I can see all of you now going, "whoa, there, Coding Hillbilly! I ain’t e’en got me a cuppa joe yet, buddy." Sorry, folks, that’s how I roll. I think slow and act fast. You gotta keep up.

The test above sets up an expectation that LoadCritters will be called on the View when we called Presenter.InitializeView. In actual fact, this isn’t being done. So here’s the poser: why does this test pass?

All right, I’ll tell ya because I like your look. And I don’t want to risk any of you actually cutting and pasting that into an IDE and coming up with some cut ‘n paste error I made. Plus I gave you a hint in the post title.

The test isn’t actually passing. When it hits view.LoadCritters, it fails silently and kicks out of the test completely. The reason bein’ SetUp isn’t being called. The reason for *that* bein’ I forgot the [ TestFixture ] attribute on the class itself.

Now as to the question on why the test doesn’t fail with a null reference exception: That’s an easy answer. I have no idea. If you add the [ TestFixture ] attribute and comment out the code that creates the view, it does fail with the expected exception.

So I’ll leave that to my loyal and, I pray, generous readers.

Kyle the Deferred

This entry was posted in MbUnit, TDD. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://codebetter.com/blogs/kyle.baley Kyle Baley

    Yeah, if you look at the name of the fixture, I’m not so sure he’d be interested in the type of hillbilly-love espoused in this particular project. But I suppose I could ask…

  • http://shane.jscconsulting.ca Shane Courtrille

    So Kyle.. I’ve got to ask. The LoveDoctor thing. Is this another effort to find Donald some sweet sweet lovin? If so you and Justice should join forces on this humanitarian project 😀

    Merry Christmas

  • http://codebetter.com/blogs/kyle.baley Kyle Baley

    Yeah, it’s probably a good thing that MbUnit allows tests without an explicit fixture. But in this case, the test is behaving badly. It should be failing with a null reference exception, yesno?

  • http://blog.bits-in-motion.com/ Jeff Brown

    This is because of MbUnit v2’s support for “naked fixtures”.

    In v3 either this feature won’t exist or we’ll put in some logic to assume the [TestFixture] declaration was there in the first place (like xUnit.Net). I don’t like the idea of making [TestFixture] optional because there are cases where you may indeed want to have [Test] methods on a class that is not a fixture. eg. It could be a mixin…