{"id":242,"date":"2010-10-26T12:07:16","date_gmt":"2010-10-26T17:07:16","guid":{"rendered":"http:\/\/scottcsims.com\/wordpress\/?p=242"},"modified":"2016-07-12T11:14:02","modified_gmt":"2016-07-12T16:14:02","slug":"use-rspec-partial-mock-to-write-a-quick-executing-example","status":"publish","type":"post","link":"http:\/\/scottcsims.com\/wordpress\/?p=242","title":{"rendered":"Use rspec partial mock to write a quick executing example."},"content":{"rendered":"<p>I wanted to test an error message in my class that processes test results and sends them to Rally. \u00a0The problem was that I did not want to actually create a rally object and run the method to parse the results, just to test the error message.<br \/>\nThis is what my class looks like.<\/p>\n<pre class=\"brush: ruby\">class AutomationRun\r\n  def send_results_to_rally\r\n    @rally = RallyUtils.new(ENV['RALLY_WORKSPACE'])\r\n    push_test_case_results\r\n    raise(\"Test Case Results Were Not Parsed Correctly\") if test_case_results.empty?\r\n    test_case_results.each do |result|\r\n      @rally.update_test_case_result(:tc_id =&gt;result.test_case_number_from_spec_report, :build =&gt;result.build_number, :verdict =&gt;result.verdict, :notes =&gt; result.note.format_note)\r\n    end\r\n  end\r\nend<\/pre>\n<p>I want to skip these steps to test the error message:<\/p>\n<ul>\n<li>don&#8217;t\u00a0instantiate\u00a0RallyUtils.new \u00a0we don&#8217;t need @rally for the test<\/li>\n<li>don&#8217;t parse the results so don&#8217;t call parse_test_case_results<\/li>\n<\/ul>\n<p>This is what my test code looks like:<\/p>\n<pre class=\"brush: ruby\"> it \"should raise an exception if there are not test case results\" do\r\n    RallyUtils.stub!(:new).and_return(nil)\r\n    automation_run=AutomationRun.new\r\n    automation_run.should_receive(:push_test_case_results).and_return([])\r\n    begin\r\n    automation_run.send_results_to_rally\r\n    rescue Exception=&gt;e\r\n    end\r\n    e.message.should == \"Test Case Results Were Not Parsed Correctly\"\r\n  end<\/pre>\n<ol>\n<li>We use a rspec stub to intercept the <strong>new<\/strong> call to the RallyUtils class and return nil. \u00a0We could have also returned a mock object if we wanted to execute a call on the rally class.\u00a0Now we are not creating a connection to Rally<\/li>\n<li>Now it is time to new up or AutomationRun class and setup our mock expectation to skip running the real push_test_case_results method.<\/li>\n<li>automation_run.should_receive tells us to intercept the symbol or method push_test_case_results<\/li>\n<li>and_return([]) will just return an empty array<\/li>\n<li>We setup a begin rescue block to capture the Exception object returned by our expected error message<\/li>\n<li>Now we call send_results_to_rally to get our error message<\/li>\n<li>Lastly we verify that the message attribute for our error is Test Case Results Were Not Parsed Correctly<\/li>\n<\/ol>\n<p>So that is it, I created a partial mock \u00a0in that we are using a real automation run object but we stubbed out one of the methods. \u00a0We also stubbed out the new call to rally so we don&#8217;t create a connection. \u00a0Now I can focus my test on exactly the function I wanted to test, the error message.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to test an error message in my class that processes test results and sends them to Rally. \u00a0The problem was that I did not want to actually create a rally object and run the method to parse the results, just to test the error message. This is what my class looks like. class [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[24,37,7],"tags":[],"class_list":["post-242","post","type-post","status-publish","format-standard","hentry","category-rally","category-rspec","category-ruby"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=242"}],"version-history":[{"count":8,"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":250,"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions\/250"}],"wp:attachment":[{"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scottcsims.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}