rspec проблема с издевательством над некоторыми объектами - PullRequest
1 голос
/ 29 марта 2011

Я вижу странное поведение, когда пытаюсь заглушить некоторые методы.Я использую рельсы 3.0.3 и rspec 2.3.0

Вот соответствующий раздел спецификации файла

    require 'spec_helper'
    include Authlogic::TestCase

    describe PrizesController do

      before do
        activate_authlogic
        @manager = Factory.create(:valid_manager, :name => "Test Manager ")
        UserSession.create @manager
      end

      def mock_prize(stubs={})
        (@mock_prize ||= mock_model(Prize, :point_cost => 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 => "37", :user_id => @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 => "37", :user_id => @manager.id
      assigns(:prize).point_cost.should eq(100)
      assigns(:prize).should be(mock_prize)
      assigns(:consumable).should be_nil
    end
  end

И метод контроллера:

 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 > @prize.point_cost
      @consumable = user.consumables.create(:kind => @prize.consumable_kind, :description => @prize.consumable_description, :redemption_message => @prize.consumable_redemption_message)
      point_record = @consumable.create_point_record(:redeemed_points => @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

Тесты не пройдены, и это вывод ...

  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)
     (#<User:0x10706b000>).available_points(any args)
         expected: 1 time
         received: 0 times
     # ./spec/controllers/prizes_controller_spec.rb:44

Есть идеи по этому поводу?Я полностью озадачен, почему два теста, вызывающие один и тот же метод с одними и теми же параметрами, будут по-разному завершаться (не говоря уже о том, что они вообще не работают ...).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...