Fixed output of a function call for a unit test

Related products: Software Factory

Code that calls a function can be difficult to test if that function performs significant logic. I want to isolate the code we are testing from the logic buried in the functions that it calls. To create independent tests, you should be able to fix the output of some functions.

For example when you have a task that confirms a sales order, you don’t want to unit-test the function that checks for a credit limit. The credit-limit check should just return ‘ok’.

Another big advantage is that this will reduce the amount of mock data required to perform the unit test. In some cases you have to fill half a database in order to get a function to return a ‘true’ whilst not even testing that function.

Great idea, the same could also apply for other subroutines.

For now, a workaround would be to add a preparation query to mock the function. Note that this requires execution as a separate command as an alter statement must be the first statement in a batch.

For instance, the preparation query could be:

exec('alter function dbo.check_credit_limit(@invoice_id int) returns bit begin return 1 end')

 


NewOpen