title: Having trouble stubbing a class method in RSpec / Rails (and using dynamic returns on stubs)
body: <p>Good evening,</p>
  
  <p>I'm trying to test a fairly long method in my "Simulation" class, which calls class methods "is_male_alive?" and "is_female_alive?" on my "Age" class a few hundred times.  The return values of these class methods are based on statistics, and I would like to stub them out to return specific values so that my tests run the same each time.</p>
  
  <p>Age.rb:</p>
  
  <pre><code>...
  
  def is_male_alive?(age)
    return false if age &gt; 119
    if (age &lt; 0 || age == nil || age == "")
      return false
    end    
    death_prob = grab_probability_male(age)
    rand_value = rand
    rand_value &gt; death_prob
  end
  
  ...
  </code></pre>
  
  <p>(the female version is essentially the same with some different constants)</p>
  
  <p>In my "simulation" class I do the following:</p>
  
  <pre><code>def single_simulation_run
  
    ...
    male_alive = Age.is_male_alive?(male_age_array[0])
    female_alive = Age.is_female_alive?(female_age_array[0])
    ...
  end
  </code></pre>
  
  <p>On each iteration of the simulation - essentially it just passes in an age (e.g. is_male_alive?(56) ) and returns true or false.</p>
  
  <p>I'd like to stub out these two methods so that:</p>
  
  <ol>
  <li>is_male_alive? returns true for any argument less than 75, false otherwise</li>
  <li>is_female_alive? returns true for any argument less than 80, false otherwise</li>
  </ol>
  
  <p>I've tried the following to see if I have the ability to stub it out (simulation_spec.rb) :</p>
  
  <pre><code>Age.should_receive(:is_male_alive?).exactly(89).times
  results = @sim.send("generate_asset_performance")
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <pre><code> Failure/Error: Age.should_receive(:is_male_alive?).exactly(89).times
     (&lt;Age(id: integer, age: integer, male_prob: decimal, female_prob: decimal) (class)&gt;).is_male_alive?(any args)
         expected: 89 times
         received: 0 times
  </code></pre>
  
  <p>I also have no idea how to set it up so that the stubbed return value is  dynamically generated based on the arguments.  Is there a way to do this with a proc?</p>
  
  <p>Is there a way to mock the entire Age class (as opposed to just mocking a single instance of the Age class?)</p>
  
  <p>Thanks for your help!!</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Looks like there is an issue with this method being called... which is really confusing. To really see if it was being called, I threw a "raise ArgumentError" into the methods.</p>
  
  <p>Development environment (console):</p>
  
  <pre><code>1.9.3p125 :003 &gt; sim = Simulation.last
  1.9.3p125 :004 &gt; sim.generate_results
    ---&gt;  ArgumentError: ArgumentError
  </code></pre>
  
  <p>So it is clearly calling this method in the development environment, as it threw the argumenterror.</p>
  
  <p>Ran this again in my tests, and it's still saying that the method was not being called...  I'm using your code below:</p>
  
  <pre><code>Age.should_receive(:is_male_alive?).with(an_instance_of(Fixnum)).at_least(:once) { |age| age &lt; 75 }
  </code></pre>
  
  <p>I have also tried this</p>
  
  <pre><code>Age.should_receive(:is_male_alive?).with(an_instance_of(Fixnum)).at_least(:once) { raise ArgumentError }
  </code></pre>
  
  <p>Any thoughts?</p>