in reply to Unit testing, context and wantarray

how do I test that a function returns nothing when called in void context?

Two thoughts:

  1. Why would it matter?

    If a function returned a value in a void context, it is just discarded.

    Not returning anything in a void context is not the point, it is not calculating the return values if they are never going to be used that makes a difference.

  2. Testing something that is obvious by inspection is a waste of time.

    If the first line of the sub is return unless defined wantarray;, the sub will not return anything when called in a void context. There is no need to test that.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Unit testing, context and wantarray
by mikeman (Acolyte) on Aug 13, 2011 at 11:22 UTC

    Thanks for your reply.

    I think you answer has pointed me in the right direction: I am trying to test for the wrong thing. I should be testing (perhaps via a mock object or similar), that my function does not do anything expensive (e.g., call a database function) to return a result when called in void context. I was erroneously focusing on the return result, rather than the operations of my function. Testing the return value (or lack thereof) would not prove if my function was performing a pointless operation.

    Thanks!

      As a general thing though, one may often call a method in a void context and expect something expensive to happen (connecting to a remote database for example), especially if you use exception handling to manage errors rather than passing error states back.

      True laziness is hard work

      I should be testing (perhaps via a mock object or similar), that my function does not do anything expensive (e.g., call a database function) to return a result when called in void context.
      Such as playing nethack? :)