开发者

Unit Test this - Simple method but don't know what's to test!

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;
}
开发者_Python百科

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!


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);

}


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜