Note that there are some explanatory texts on larger screens.

plurals
  1. POrspec problem with mocking some objects
    primarykey
    data
    text
    <p>I'm seeing some strange behavior when I'm trying to stub out some methods. I'm using rails 3.0.3 and rspec 2.3.0</p> <p>Here is the relevant section of the spec file</p> <pre><code> require 'spec_helper' include Authlogic::TestCase describe PrizesController do before do activate_authlogic @manager = Factory.create(:valid_manager, :name =&gt; "Test Manager ") UserSession.create @manager end def mock_prize(stubs={}) (@mock_prize ||= mock_model(Prize, :point_cost =&gt; 100).as_null_object).tap do |prize| prize.stub(stubs) unless stubs.empty? end end def mock_consumable(stubs={}) (@mock_consumable ||= mock_model(Consumable).as_null_object).tap do |consumable| consumable.stub(stubs) unless stubs.empty? end end describe "GET buy_this" do it "assigns the requested prize as @prize and requested consumable as @consumable if the player has enough points" do Prize.stub(:find).with("37") { mock_prize } @manager.should_receive(:available_points).and_return(1000) get :buy_this, :id =&gt; "37", :user_id =&gt; @manager.id assigns(:prize).point_cost.should eq(100) assigns(:prize).should be(mock_prize) assigns(:consumable).should_not be_nil end it "assigns the requested prize as @prize and no consumable as @consumable if the player does not have enough points" do Prize.stub(:find).with("37") { mock_prize } @manager.should_receive(:available_points).and_return(10) get :buy_this, :id =&gt; "37", :user_id =&gt; @manager.id assigns(:prize).point_cost.should eq(100) assigns(:prize).should be(mock_prize) assigns(:consumable).should be_nil end end </code></pre> <p>And the controller method:</p> <pre><code> def buy_this @prize = Prize.find(params[:id]) user = User.find(params[:user_id]) if params[:user_id] user ||= current_user flash[:notice] = ("Attempting to redeem points for a prize") if user.available_points &gt; @prize.point_cost @consumable = user.consumables.create(:kind =&gt; @prize.consumable_kind, :description =&gt; @prize.consumable_description, :redemption_message =&gt; @prize.consumable_redemption_message) point_record = @consumable.create_point_record(:redeemed_points =&gt; @prize.point_cost) point_record.user = user point_record.save flash[:success] = "You successfully redeemed #{@prize.point_cost} points for #{@prize.name}" else flash[:error] = "Sorry, you don't seem to have enough points to buy this" end redirect_to prizes_path end </code></pre> <p>The tests fail and this is the output...</p> <pre><code> 1) PrizesController GET buy_this assigns the requested prize as @prize and requested consumable as @consumable if the player has enough points Failure/Error: assigns(:consumable).should_not be_nil expected not nil, got nil # ./spec/controllers/prizes_controller_spec.rb:39 2) PrizesController GET buy_this assigns the requested prize as @prize and no consumable as @consumable if the player does not have enough points Failure/Error: @manager.should_receive(:available_points).and_return(10) (#&lt;User:0x10706b000&gt;).available_points(any args) expected: 1 time received: 0 times # ./spec/controllers/prizes_controller_spec.rb:44 </code></pre> <p>Any ideas about this? I'm totally stumped why the two tests calling the same method with the same parameters would fail in different ways (not to mention, I don't understand why they are failing at all...).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload