17 July, 2006

Is a test without an assert a test?

Right now I’m reading a, so far, good article on the MVP pattern. The article Model View Presenter at MSDN Mag. The author is taking a TDD approach and the first test he writes looks like this:

publicvoid ShouldLoadListOfCustomersOnInitialize()
    mockery = new Mockery();
    ICustomerTask  mockCustomerTask = mockery.NewMock();
    IViewCustomerView  mockViewCustomerView = mockery.NewMock();
    ILookupList  mockCustomerLookupList = mockery.NewMock();

    ViewCustomerPresenter presenter = new ViewCustomerPresenter(mockViewCustomerView, mockCustomerTask);

    ILookupCollection mockLookupCollection = mockery.NewMock();

    Expect.Once.On(mockCustomerTask).Method( "GetCustomerList").Will(Return.Value(mockLookupCollection));
    Expect.Once.On(mockViewCustomerView).GetProperty( "CustomerList").Will(Return.Value(mockCustomerLookupList));
    Expect.Once.On(mockLookupCollection).Method( "BindTo").With(mockCustomerLookupList);


Except for noticing the nice fluent interface of NMock2 the thing that struck me was that there is no Assert! I have never written a test without at least one assert to confirm. I’m not saying that it’s wrong to skip the assert. I haven’t had any time to look at NMock2 but I guess he missed a .Verify() at the end.
UPDATE 2006-07-14: My bad about the article author missing out on the .Verify statement. After reviewing the code a little I found it in the TearDown: mockery.VerifyAllExpectationsHaveBeenMet();.

So that means you’ll get an exception if all methods and properties are not accessed, but you don’t have an assert to confirm that the test is ok. Hmm, I don’t know, but it feels wrong. Any other opinions on this matter? Post a comment!

Tags: ,