{"id":54,"date":"2015-02-10T09:33:34","date_gmt":"2015-02-10T09:33:34","guid":{"rendered":"http:\/\/codereporter.de\/?p=54"},"modified":"2015-02-10T09:33:34","modified_gmt":"2015-02-10T09:33:34","slug":"rspec-how-to-stub-a-random-variable-make-the-weather-as-you-need-it-to-be","status":"publish","type":"post","link":"https:\/\/codereporter.de\/?p=54","title":{"rendered":"RSpec: How to stub a random variable (make the weather as you need it to be)"},"content":{"rendered":"<p>In our last challenge there was an airport that gave or declined landing permission for planes based on the weather. There was no real weather forecast, I just generated a random weather:<\/p>\n<p><code> def generate_weather<br \/>\n[:sunny, :sunny,\u00a0:sunny, :stormy].shuffle.first<br \/>\nend<br \/>\n<\/code><\/p>\n<p>The Ruby-method .shuffle randomly mixes the values within the array and .first than takes the first value out.<\/p>\n<h3>RSpec test: force the function return a specific value<\/h3>\n<p>I implemented an if-statement in my landing- and take-off-procedures. So that a plane could only land if weather was sunny. But the if-statement made the tests randomly fail I had written to check those methods. Very annoying. Red type all over my terminal.<\/p>\n<p>Luckily RSpec gives you the <a href=\"https:\/\/relishapp.com\/rspec\/rspec-mocks\/v\/2-4\/docs\/method-stubs\">possibility to stub methods<\/a>. I didn&#8217;t quite understand how that works. But now that I have understood I try to explain it out of a beginner&#8217;s perspective:<\/p>\n<p>If I want my generate_weather method (above) for testing-purposes to return always :stormy, I set the following line in the beginning of my RSpec-Test:<\/p>\n<p><code> expect(plane).to receive(:generate_weather).and_return(:stormy)<br \/>\n<\/code><\/p>\n<p>Translation: If the method generate_weather is sent to the object plane, it will return :stormy &#8211; no matter what generate_weather actually generated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last challenge there was an airport that gave or declined landing permission for planes based on the weather. There was no real weather forecast, I just generated a random weather: def generate_weather [:sunny, :sunny,\u00a0:sunny, :stormy].shuffle.first end The Ruby-method .shuffle randomly mixes the values within the array and .first than takes the first value&hellip; <a class=\"more-link\" href=\"https:\/\/codereporter.de\/?p=54\"><span class=\"screen-reader-text\">RSpec: How to stub a random variable (make the weather as you need it to be)<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":55,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[7,9,6],"_links":{"self":[{"href":"https:\/\/codereporter.de\/index.php?rest_route=\/wp\/v2\/posts\/54"}],"collection":[{"href":"https:\/\/codereporter.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codereporter.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codereporter.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codereporter.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":1,"href":"https:\/\/codereporter.de\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":56,"href":"https:\/\/codereporter.de\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/56"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codereporter.de\/index.php?rest_route=\/wp\/v2\/media\/55"}],"wp:attachment":[{"href":"https:\/\/codereporter.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codereporter.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codereporter.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}