1

Is there a scenario where you would not want to return a value from a function?

Lets say the function I am writing an integration/unit test for looks like this

function functionBeingTested() {
    functionThatIsBeingStubbed();
}

I need to see if functionThatIsBeingStubbed() is getting called and my intuition is that I can just return it, and the value returned will return either true if the arguments pass, or a string that will be the error message if not. Like this;

function functionBeingTested() {
    return functionThatIsBeingStubbed();
}

Is there value in doing this all the time? Is there a scenario where you wouldn't want to return a function that isn't actually needing to be returned for any reason other then testing? Is there a better way? Is returning a string with the error message from the stub a good idea or is there a better way? I am new to testing and am not sure if this is good practice or not.

  • Can you give us a better example? Your questions are very generic. I am not sure that I understand your question. Take a look at this. Maybe it's helpful. https://stackoverflow.com/questions/32268019/unit-testing-of-a-function-that-doesnt-return-any-value – Eleftherios Jan 05 '19 at 18:14
  • possibly look into sinon for stubs and mocks: https://sinonjs.org/releases/v7.1.1/ . This allows you to check call counts and such. – Andrew Nolan Jan 07 '19 at 19:49
  • @AndrewNolan Thank you, this was exactly what I was looking for! – Bedroom Shredder Jan 07 '19 at 20:03

0 Answers0