testing-in-bash icon indicating copy to clipboard operation
testing-in-bash copied to clipboard

Function mocking by export in shunit2

Open yman45 opened this issue 4 years ago • 1 comments

Your shunit2 description mentions that it can't be used to mock functions by export. In my experience it's working though. Looks like alias in your unit test example is still in effect and break function export test. This patch makes tests work for me.

diff --git a/example-shunit2/test/unit_test.sh b/example-shunit2/test/unit_test.sh
index 82176e0..38d64ed 100755
--- a/example-shunit2/test/unit_test.sh
+++ b/example-shunit2/test/unit_test.sh
@@ -20,12 +20,10 @@ function test_there_should_be_20_percent_discount_for_large_quantities_on_odd_da
 
   assertEquals 0 $?
   assertContains "${result}" "Total 320"
+  unalias getDay
 }
 
 function test_there_should_be_no_discount_even_for_large_quantities_on_even_days() {
-  # Mocking by exporting functions does not work for shunit2
-  startSkipping
-
   source src/ice_cream_price.sh
 
   # Mocking a function by exporting a function

And by the way, thanks for the great guide!

yman45 avatar Sep 12 '21 12:09 yman45

Hey @yman45 ,

Thanks for the correction! I'll check and update the guide!

Cheers, David

dodie avatar Sep 15 '21 16:09 dodie