chefspec: Stub library problem
I’m unsure if it’s a bug or a bad usage on my part. I’m trying to stub a cookbook library class method. I read all old closed issues about stubbing libraries and implemented ideas from lots of them, but I’m just unable to make it work correctly.
The stub is correct when called in the before block, in the runner new block and in the example directly. But when called within the recipe file default.rb, the original method is called. I really don’t understand why it’s not working.
I’m willing to debug this, so any insights or ideas is welcome.
You can check a minimal test case here: https://github.com/maoueh/chefspec-stub-problem
Regards, Matt
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 17 (9 by maintainers)
Let me write a note here
CAUSE
When
ChefSpec::SoloRunner.convergeis called,Chef::Client#setup_run_contextwill be called atand, blah blah, it will finally
Kernel.loadlibraries atChef::RunContext::CookbookCompiler#load_libraries_from_cookbookNotice that it is
load, notrequire.So, when you write libraries and spec:
Here, you are stubbing MyHelper.function, but
Inside
converge, libraries are loaded again, and the stubbing is overwritten by real implemntationHOW TO RESOLVE
Write libraries as following not to be loaded again: