{"action":"created","issue":{"url":"https://api.github.com/repos/sethvargo/chefspec/issues/256","labels_url":"https://api.github.com/repos/sethvargo/chefspec/issues/256/labels{/name}","comments_url":"https://api.github.com/repos/sethvargo/chefspec/issues/256/comments","events_url":"https://api.github.com/repos/sethvargo/chefspec/issues/256/events","html_url":"https://github.com/sethvargo/chefspec/issues/256","id":22200806,"number":256,"title":"Problems with stub_command on only_if block","user":{"login":"adamjk-dev","id":4096455,"avatar_url":"https://avatars.githubusercontent.com/u/4096455?v=3","gravatar_id":"","url":"https://api.github.com/users/adamjk-dev","html_url":"https://github.com/adamjk-dev","followers_url":"https://api.github.com/users/adamjk-dev/followers","following_url":"https://api.github.com/users/adamjk-dev/following{/other_user}","gists_url":"https://api.github.com/users/adamjk-dev/gists{/gist_id}","starred_url":"https://api.github.com/users/adamjk-dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamjk-dev/subscriptions","organizations_url":"https://api.github.com/users/adamjk-dev/orgs","repos_url":"https://api.github.com/users/adamjk-dev/repos","events_url":"https://api.github.com/users/adamjk-dev/events{/privacy}","received_events_url":"https://api.github.com/users/adamjk-dev/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":20,"created_at":"2013-11-06T15:20:00Z","updated_at":"2015-08-25T22:43:25Z","closed_at":"2013-11-06T15:28:26Z","body":"I had a recipe with a resource that had the following only_if block:\r\n```ruby\r\nonly_if {File.exists?(install_file) && !File.exists?(\"#{install_dir}/bin/java\")} #If the installer file exists, run it\r\n```\r\n\r\nI tried to do a stub_command, but could not get it to work. The test (checking for a run_execute) kept failing without warning me to add a stubbed command to make that true. \r\n\r\nIs there any syntax support for the above only_if block in a stub_command?\r\n\r\nAs a workaround, I had to change my only_if block and stub_command to the following:\r\n```ruby\r\nonly_if \"test -e #{install_file} && test ! -e #{install_dir}/bin/java\" #If the installer file exists, run it\r\n\r\nstub_command(\"test -e /tmp/jrockit-jdk1.6.0_31-R28.2.3-4.1.0-linux-x64.bin && test ! -e /opt/weblogic/java/jrmc-1.6.0_31/bin/java\").and_return(true)\r\n```"},"comment":{"url":"https://api.github.com/repos/sethvargo/chefspec/issues/comments/134762539","html_url":"https://github.com/sethvargo/chefspec/issues/256#issuecomment-134762539","issue_url":"https://api.github.com/repos/sethvargo/chefspec/issues/256","id":134762539,"user":{"login":"flatrocks","id":125421,"avatar_url":"https://avatars.githubusercontent.com/u/125421?v=3","gravatar_id":"","url":"https://api.github.com/users/flatrocks","html_url":"https://github.com/flatrocks","followers_url":"https://api.github.com/users/flatrocks/followers","following_url":"https://api.github.com/users/flatrocks/following{/other_user}","gists_url":"https://api.github.com/users/flatrocks/gists{/gist_id}","starred_url":"https://api.github.com/users/flatrocks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flatrocks/subscriptions","organizations_url":"https://api.github.com/users/flatrocks/orgs","repos_url":"https://api.github.com/users/flatrocks/repos","events_url":"https://api.github.com/users/flatrocks/events{/privacy}","received_events_url":"https://api.github.com/users/flatrocks/received_events","type":"User","site_admin":false},"created_at":"2015-08-25T22:43:25Z","updated_at":"2015-08-25T22:43:25Z","body":"Possibly related, hopefully helpful.  Using a similar only_if guard,\r\n``` ruby\r\nonly_if { Dir.glob('my_dir/*'), 0).empty? }\r\n```\r\nI tried stubbing out the Dir.glob to return an empty, or non-empty array, ala:\r\n``` ruby\r\nallow(Dir).to receive(:glob).with('/my_dir/*', 0).and_return []\r\nallow(Dir).to receive(:glob).with('/my_dir/*', 0).and_return ['something_here']\r\n```\r\nHowever the script ALWAYS ran.\r\n\r\nI realized that the Dir.glob stub was getting hit twice, but only returned my specified return value the first time.  (This may simply show I am missing something about the \"new\" rspec mock/stub usage, but...) my solution turned out to be simple:\r\n``` ruby\r\nallow(Dir).to receive(:glob).with('/my_dir/*', 0).at_least(:once).and_return []\r\n```\r\nWith the ```at_least(:once)```, the glob stub returned the expected value _each_ time, and the test ran as expected.\r\n\r\n\r\n__The key to figuring this out was in splitting up the guard to dump some debug info__, changing this,\r\n``` ruby\r\nonly_if { Dir.glob('/my_dir/*', 0).empty? }\r\n```\r\nto this:\r\n``` ruby\r\nonly_if do\r\n    puts Dir.glob('/my_dir/*', 0)\r\n    puts Dir.glob('/my_dir/*', 0).empty?\r\n    Dir.glob('/my_dir/*', 0).empty?\r\nend\r\n```\r\n...to see what the heck was going on.  Mine was clearly a problem with my use of rspec, and not chefspec at all, but that was not clear until I'd solved it.  I suspect the same for this issue and #250."}}