【问题标题】:RSpec on Controller and Stubbing控制器和存根上的 RSpec
【发布时间】:2011-03-11 04:51:01
【问题描述】:

我对使用 rspec 很陌生,我正在尝试为我的控制器编写测试。我有这个控制器(我正在使用 mocha 进行存根):

class CardsController < ApplicationController
  before_filter :require_user

  def show
    @cardset = current_user.cardsets.find_by_id(params[:cardset_id])

    if @cardset.nil?
      flash[:notice] = "That card doesn't exist. Try again."
      redirect_to(cardsets_path)
    else
      @card = @cardset.cards.find_by_id(params[:id])
    end
  end
end

我正在尝试用这样的东西来测试这个动作:

describe CardsController, "for a logged in user" do
  before(:each) do
    @cardset = Factory(:cardset)
    profile = @cardset.profile
    controller.stub!(:current_user).and_return(profile)
  end

  context "and created card" do
    before(:each) do
      @card = Factory(:card)
    end

    context "with get to show" do
      before(:each) do
        get :show, :cardset_id => @cardset.id, :id => @card.id
      end

      context "with valid cardset" do
        before(:each) do
          Cardset.any_instance.stubs(:find).returns(@cardset)
        end

        it "should assign card" do
          assigns[:card].should_not be_nil
        end

        it "should assign cardset" do
          assigns[:cardset].should_not be_nil
        end

      end
    end
  end
end

“应该分配卡组”测试通过,但我不知道如何正确地为“应该分配卡”测试正确地存根@card = @cardset.cards.find_by_id(params[:id]) 这一行。测试此操作的最佳方法是什么,或者如果我在正确的轨道上,我将如何正确地存根我的模型调用?

【问题讨论】:

    标签: ruby-on-rails unit-testing rspec mocha.js stubbing


    【解决方案1】:

    我最终在哪里寻找的存根

    Cardset.stubs(:find_by_id).returns(@cardset)
    @cardset.cards.stubs(:find_by_id).returns(@card)
    

    【讨论】:

    • stubs 方法在 RSpec 2.8 中对我不起作用,但 stub 是。遇到此问题的任何其他人都可以尝试此修订版:Cardset.stub(:find).and_return(@cardset).
    【解决方案2】:

    好的,删除了一个错误的答案。

    首先:你在存根 find 而不是 find_by_id。尽管您不需要使用 find_by_id,因为这是 find 的默认设置。所以使用find

    第二:before :each 排序将在你存根Cardset 之前调用get :show

    第三:检查您的 test.log 并确保您没有被重定向。您的 require_user 操作可能会在设置 current_user 之前导致重定向。

    class CardsController < ApplicationController
      ...
         @card = @cardset.cards.find(params[:id])
      ...
    end
    
    describe CardsController, "for a logged in user" do
      before(:each) do
        @cardset = Factory(:cardset)
        profile = @cardset.profile
        controller.stub!(:current_user).and_return(profile)
      end
    
      context "and created card" do
        before(:each) do
          @card = Factory(:card)
        end
    
        context "with get to show" do
    
          context "with valid cardset" do
            before(:each) do
              Cardset.any_instance.stubs(:find).returns(@cardset)
              get :show, :cardset_id => @cardset.id, :id => @card.id
            end
    
            it "should assign card" do
              assigns[:card].should_not be_nil
            end
    
            it "should assign cardset" do
              assigns[:cardset].should_not be_nil
            end
    
          end
        end
      end
    end
    

    【讨论】:

    • 好的,谢谢,我会检查这些东西。我使用 find_by_id 的原因是,当找不到记录时,它会返回 nil 而不是抛出异常,这在这种情况下似乎更容易处理。
    • 我将我的代码更新为此处显示的内容:gist.github.com/461667 我仍然得到一个失败的“应该分配卡”测试,尽管我被 before_filter 重定向,但你是正确的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2013-07-28
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多