1

Im new at TDD and i still have many questions, and one of this questions is how to test a void method!

I have this one with params passed by refference, and i have no clue how to test: (this is just a test, not my real method)

public void ReplaceCenter(ref string cod, ref string name)
    {
        User user = userDAO.GetSomething();

        if (user.Cod != null)
        {
            cod = user.Cod;
        }

        if (user.Name != null)
        {
            name = user.Name;
        }
    }            

Can someoen help? Thanks!

Otuyh
  • 2,384
  • 5
  • 22
  • 40

4 Answers4

5

Assuming userDAO was injected as a dependency and can be mocked I would do the following tests:

  1. Assert GetSomething() was called on mocked userDAO
  2. Assert returned reference string cod == passed in User.Cod that we used to create mocked userDAO
  3. Assert returned reference string name == passed in User.Cod that we used to create mocked userDAO

I agree with avoiding ref parameters though, I would suggest refactoring as suggested in the other answer by @Tim Cools

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • This concept is not so clear to me right now.. How can i mock the dao that is inside my method, in my test? How im gonna mock the result to use inside the method? Because i cant pass it by params, hes not one of them! Sorry, im still very noob! – Otuyh Feb 16 '12 at 19:44
  • you can pass a mocked `userDAO` as a constructor parameter to the class that contains the `ReplaceCenter` method – BrokenGlass Feb 16 '12 at 19:46
3

you should try to avoid ref and out parameters. use an object that contains the two properties. this would make your design cleaner and your job much cleaner...

edit: if you really want to moq the out parameters use you can find an example here

Community
  • 1
  • 1
Tim Cools
  • 1,162
  • 10
  • 19
1

Just test the values in the params after you call the method, mocking the userDAO to control what it returns (null, cod, name)

Jaime
  • 6,736
  • 1
  • 26
  • 42
  • And if they are not passed by refference? Could i still do it? – Otuyh Feb 16 '12 at 19:44
  • If you're not passing them by reference, are you returning anything? if so, test against the return value(s) if it's something that only operates on other things, then you test that those dependencies are called with the right params – Jaime Feb 16 '12 at 21:00
0

A void method implies that there is going to be some sort of side-effect. I generally suggest avoiding them, if possible, but when you do need to test one, general rules of thumb would be:

  1. Assert all expected calls on your mocks were made.
  2. If possible, assert that the mocks were called with specific parameter values. This is more important for void methods, because you are testing the side-effects.
  3. Assert every side-effect you can, including those that should not have changed.

Really, these assertions should be made for non-void methods, as well, perhaps more to prevent unexpected side-effects than to test for expected ones. (IMO, a function should have zero side-effects if at all possible.)

Steve Czetty
  • 6,147
  • 9
  • 39
  • 48