【问题标题】:RSPEc - undefined method `stubs' for #<RoomsController:0x0000010534c050>RSPEc - #<RoomsController:0x0000010534c050> 的未定义方法“存根”
【发布时间】:2011-08-26 21:31:36
【问题描述】:

我正在尝试为我的 Rooms 控制器编写一个 rspec,以使用 CanCan 测试权限,但标题中不断出现错误。我正在按照控制器测试下的步骤进行操作:https://github.com/ryanb/cancan/wiki/Testing-Abilities

room_controller_spec.rb

require 'spec_helper'

describe RoomsController do

  before(:each) do
    @user_1 = Factory.create(:user, :password => 'password')
    @room_for_user_1 = Room.create(:user_id => @user_1.id)

    @ability = Object.new
    @ability.extend(CanCan::Ability)
    @controller.stubs(:current_ability).returns(@ability)
  end

  describe "Room Permissions" do

    it "should allow a user to join a room" do
      @ability.can :show, @room_for_user_1
      get :show, { :uuid => @room_for_user_1.uuid }
      response.should render_template("show")
    end

  end

end

关于如何让 devise + CanCan + RSpec 工作以便我可以测试控制器的任何建议?谢谢

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 rspec devise cancan


【解决方案1】:

这不是 RSpec 语法,你想要的是:

@controller.stub!(:current_ability).and_return(@ability)

【讨论】:

  • 这似乎已经修复了该错误。知道为什么我首先需要存根吗?
【解决方案2】:

较新的版本 3.8 allow 语法。

allow( @controller ).to receive( :current_ability ).and_return( @ability )

根据the Relish 3.8 docs

【讨论】:

    猜你喜欢
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多