开发者

Unittesting a Save function using MVP pattern and RhinoMock

开发者 https://www.devze.com 2023-03-11 00:17 出处:网络
I am trying to get a better code coverage with my unittests, and recently I switched to using RhinoMock for my Mocking needs.

I am trying to get a better code coverage with my unittests, and recently I switched to using RhinoMock for my Mocking needs.

But I have got a question with how to write a specific unit-test, the Save() function.

I have an IView interface with several functions to retrieve values from the view (aspx page), examples are GetUsername(), GetPassword(), GetAddress() and GetCountry().

When the user clicks the submit button I wan开发者_如何学编程t to have tests that tests if all these functions are actually being called. So I wrote this test:

    [TestMethod]
    public void MainController_Save_ShouldRetrieveLUsername()
    {
        //Initialize the IView and Controller
        InitViewAndController();

        //Trigger the Save function triggering the controller 
        //to collect information for storage
        _controller.Save();

        _view.AssertWasCalled(s => s.GetUsername(), o => o.Repeat.Once());
    }

Now finally comes the question, considering the aspx contains 15 input fields that needs to be saved, is there a better way to test this behaviour that writing and maintaining 15 of these tests? On one hand test should be simple and optimally only one Assert, but 15 of these functions feels like a waste.


Instead of testing whether these functions are called (they look more like properties instead of functions, BTW), you should check the results of the Save function. You should treat your test code more like a black box and try not to insert too much of its interior knowledge into your tests. This way your tests will be less brittle when the Save code changes.

Google for "state based testing".

0

精彩评论

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

关注公众号