title: Mocking/stubbing system call using RSPec
body: <p>This is helper module:</p>
  
  <pre><code>module SendingMailHelper
    def send_mail(subject, body)
      to_email_id = " abc@gmail.com"
      cc_email_id = "def@gmail.com"
      html_message = %{&lt;html&gt;&lt;body&gt;#{body}&lt;/body&gt;&lt;/html&gt;}
      flag=false
      while(!flag) do
        flag = system %{echo "#{html_message}" | mutt -e "set content_type=text/html" -s "#{subject}" #{cc_email_id} -- #{to_email_id}}
      end
      flag
    end
  end
  </code></pre>
  
  <p>The way I have written my spec is as follows, but it is not working:</p>
  
  <pre><code>require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  
  describe SendingMailHelper do
    it "test something" do
      html_message = %{&lt;html&gt;&lt;body&gt;www.google.com&lt;/body&gt;&lt;/html&gt;}
      to_email_id = " abc@gmail.com"
      cc_email_id = "def@gmail.com"
      subject = "test e-mail"
      SendingMailHelper.expects(:system).with(%{echo "#{html_message}" | mutt -e "set content_type=text/html" -s "#{subject}" #{cc_email_id} -- #{to_email_id}}).returns(true).once
      helper.send_mail("test e-mail","www.google.com").should==true
    end
  end
  </code></pre>
  
  <p>Getting the following error:</p>
  
  <pre><code>SendingMailHelper test something
       Failure/Error: SendingMailHelper.expects(:system).with(%{echo "#{html_message}" | mutt -e "set content_type=text/html" -s "#{subject}" #{cc_email_id} -- #{to_email_id}}).returns(true).once
       Mocha::ExpectationError:
         not all expectations were satisfied
         unsatisfied expectations:
         - expected exactly once, not yet invoked:
  </code></pre>
  
  <p><strong>I also want to mock it in such a way that mocked mutt returns <code>false</code> twice and true in the third call to test the retry mechanism. Is there a way to do that?</strong></p>