模仿一些对象的rspec问题

时间:2011-03-28 22:58:23

标签: ruby-on-rails ruby-on-rails-3 controller rspec

当我试图找出一些方法时,我看到了一些奇怪的行为。我正在使用rails 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

有关于此的任何想法?我完全难以理解为什么调用具有相同参数的相同方法的两个测试会以不同方式失败(更不用说,我不明白它们为什么会失败...)。

0 个答案:

没有答案