简体   繁体   English

RSpec关于控制器和存根

[英]RSpec on Controller and Stubbing

I am pretty new to using rspec and am trying to write my tests for my controllers. 我对使用rspec很陌生,并且正在尝试为我的控制器编写测试。 I have this controller (I am using mocha for stubbing): 我有这个控制器(我正在使用摩卡进行存根):

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

I am trying to test this action with something like this: 我正在尝试通过以下方式测试此操作:

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

The "should assign cardset" test passes, but I cannot figure out how to properly stub this line @card = @cardset.cards.find_by_id(params[:id]) for the "should assign card" test. “应该分配卡集”测试通过了,但是我无法弄清楚如何正确地对这行@card = @cardset.cards.find_by_id(params[:id])进行“应该分配卡集”测试。 What is the best way of testing this action, or if I'm on the right track how would I properly stub my Model calls? 测试此操作的最佳方法是什么,或者如果我走对了,我将如何正确处理我的模型调用?

Ok, deleted a previous answer which was wrong. 好的,删除以前的答案是错误的。

First: you are stubbing find not find_by_id . 首先:您正在存根find not find_by_id Although you don't need to use find_by_id since that is the default for find. 尽管您不需要使用find_by_id,因为这是find的默认设置。 So use find 所以使用find

Second: the before :each ordering is going to call the get :show before the you stub Cardset 第二: before :each订购都将在存根Cardset之前调用get :show

Third: check your test.log and make sure you are not getting redirected. 第三:检查您的test.log并确保您没有被重定向。 Your require_user action might cause a redirect before current_user is even set. 您的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

The stubs that I ended up looking for where these 我最终找到的存根

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM