views:

89

answers:

2

a very simple method, but don't know what's to test!

I'd like to test this method in Business Logic Layer, and the _dataAccess apparently is from data layer.

public DataSet GetLinksByAnalysisId(int analysisId)
{
        DataSet result = new DataSet();
        result = _dataAccess.SelectAnalysisLinksOverviewByAnalysisId(analysisId);
        return result;
}

All Im testing really is to test _dataAccess.SelectAnalysisLinksOverviewByAnalysisId() is get called!

here's my test code (using Rhino mock)

[TestMethod]
public void Test()
{
   var _dataAccess = MockRepository.GenerateMock<IDataAccess>();

   _dataAccess.Expect(x => x.SelectAnalysisLinksOverviewByAnalysisId(0));

   var analysisBusinessLogic = new AnalysisLinksBusinessLogic(_dataAccess);
   analysisBusinessLogic.GetLinksByAnalysisId(0);

   _dataAccess.VerifyAllExpectations();

}

Let me know if you writing the test for this method what would you test against?

Many Thanks!

+2  A: 

Your proposed example test doesn't test the method in question, but rather an overload with the same name.

Another issue is that the expectation set up for the mock doesn't match the method called. Again, it's another overload.

The third thing that comes into my mind is that you don't need to perform an interaction-based test here. Since the method under test has a return value, you can perform a simple state-based test.

Here's one possible alternative:

[TestMethod]
public void Test()
{
   var id = 1;
   var expectedResult = new DataSet();

   var dataAccess = MockRepository.GenerateStub<IDataAccess>();

   dataAccess.Stub(x => x.SelectAnalysisLinksOverviewByAnalysisId(1))
       .Return(expectedResult);

   var analysisBusinessLogic = new AnalysisLinksBusinessLogic(dataAccess);
   var result = analysisBusinessLogic.GetLinksByAnalysisId(id);

   Assert.AreEqual(expectedResult, result);

}
Mark Seemann
thaks so much for the answer, firstly let me correct my original test (there not meant to be any overload, my bad....)only question I'd like to ask now is - the expectedResult is from the mock. what's the point to Assert.AreEqual(expectedResult, result) ?or lets day the only thing it's testing really is make sure _dataAccess.SelectAnalysisLinksOverviewByAnalysisId() get called?
Luc
It does test a little more than that, because it also tests that SelectAnalysisLinksOverviewByAnalysisId gets called with the correct value. That may not seem like a lot, but it's about 100% of the method covered.
Mark Seemann
awesome, Cheers Mark!
Luc
small point, but should you not be calling `GenerateStub` rather than `GenerateMock` as you are not verifying any expectations on the object, simply using it to provide data for the test.
Sam Holder
@Sam Holder: You are right. Good catch. Corrected.
Mark Seemann
A: 

actually what you think this little tweak?

dataAccess.Expect(x =>
x.SelectAnalysisLinksOverviewByAnalysisId(1)).Return(expectedResult);


 Assert.AreEqual(expectedResult, result);
 dataAccess.VerifyAllExpectations();

that way we doing the Assert as well as expect the SelectAnalysisLinksOverviewByAnalysisId() being called

What's your thoughts?

Luc
yeah I think you could do this. And I think you probably want to, as without it it would be possible to refactor the `analysisBusinessLogic.GetLinksByAnalysisId(id)` call to return a new DataSet (equal to the one created in the test) without calling the expected method and the test would still pass, but the functionality would be broken.
Sam Holder