{"id":7046,"date":"2014-05-17T00:24:37","date_gmt":"2014-05-17T00:24:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/why-is-my-test-double-not-expecting-the-command-i-allowed-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:24:37","modified_gmt":"2014-05-17T00:24:37","slug":"why-is-my-test-double-not-expecting-the-command-i-allowed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/why-is-my-test-double-not-expecting-the-command-i-allowed-collection-of-common-programming-errors\/","title":{"rendered":"Why is my test double not expecting the command I allowed?-Collection of common programming errors"},"content":{"rendered":"<p>I have some code which makes shellout calls to the Linux OS, which will run distro-specific commands. I&#8217;m trying to ensure the tests can be run on any system, so am using a test double for the <code>Mixlib::ShellOut<\/code> call. Here&#8217;s a simplified version that replicates my issue:<\/p>\n<pre><code>require 'mixlib\/shellout'\nclass SelinuxCommand\n  def run\n    runner = Mixlib::ShellOut.new('getenforce')\n    runner.run_command\n  end\nend\n<\/code><\/pre>\n<p>My test stubs <code>Mixlib:ShellOut.new<\/code> returning a test double, and then says that <code>:run_command<\/code> should return the string <code>'Enforcing'<\/code>:<\/p>\n<pre><code>require 'rspec'\nrequire_relative 'selinuxcommand'\ndescribe SelinuxCommand do\n  it 'gets the Selinux enforcing level' do\n    command = SelinuxCommand.new\n    Mixlib::ShellOut.stub(:new).and_return(double)\n    allow(double).to receive(:run_command).and_return('Enforcing')\n    expect command.run.to eql 'Enforcing'\n  end\nend\n<\/code><\/pre>\n<p>However, when I run the test I see:<\/p>\n<pre><code>$ rspec -fd selinuxcommand_spec.rb\n\nSelinuxCommand   gets the Selinux enforcing level (FAILED - 1)\n\nFailures:\n\n  1) SelinuxCommand gets the Selinux enforcing level\n     Failure\/Error: expect command.run.to eql 'Enforcing'\n       Double received unexpected message :run_command with (no args)\n     # .\/selinuxcommand.rb:5:in `run'\n     # .\/selinuxcommand_spec.rb:9:in `block (2 levels) in '\n\nFinished in 0.00197 seconds 1 example, 1 failure\n\nFailed examples:\n\nrspec .\/selinuxcommand_spec.rb:5 # SelinuxCommand gets the Selinux enforcing level\n<\/code><\/pre>\n<p>I don&#8217;t understand why the double doesn&#8217;t expect <code>:run_command<\/code> when I explicitly set it up to expect that. What have I missed?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have some code which makes shellout calls to the Linux OS, which will run distro-specific commands. I&#8217;m trying to ensure the tests can be run on any system, so am using a test double for the Mixlib::ShellOut call. Here&#8217;s a simplified version that replicates my issue: require &#8216;mixlib\/shellout&#8217; class SelinuxCommand def run runner = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7046","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7046"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7046\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}