Quantcast
Channel: User Ja͢ck - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by Ja͢ck for Trying to test my randomiser in rspec by passing specific numbers to it

$
0
0

Your weather_randomiser does too many things:

  1. It generates a random number
  2. It maps a random number to a string value
  3. It updates an instance variable

To make things more testable, you could split those out, e.g.:

def weather_randomiser  rand(1..10)enddef set_weather(value)  @weather = case value    when 1..8 then 'sunny'    when 9..10 then 'stormy'  endenddef stormy?  set_weather weather_randomiser  @weather == 'stormy'end

It does introduce an awkward coupling between the randomiser and setter; changing the former requires looking at the latter ... ymmv


Viewing all articles
Browse latest Browse all 44


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>