RSPEc - 未定义的方法“存根”;对于#

发布于 2024-12-01 10:10:50 字数 924 浏览 0 评论 0原文

我正在尝试为我的 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 工作以便测试控制器有什么建议吗?谢谢

I'm trying to write an rspec for my Rooms controller to test permissions with CanCan but keep getting the error in the title. I'm following the steps here under Controller Testing: 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

Any advice on how I can get devise + CanCan + RSpec working so I can test the controller? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2024-12-08 10:10:50

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

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

That's not RSpec syntax, what you want is:

@controller.stub!(:current_ability).and_return(@ability)
梦年海沫深 2024-12-08 10:10:50

较新版本 3.8 allow 语法。

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

根据 Relish 3.8 文档

Newer Version 3.8 allow Syntax.

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

As per the Relish 3.8 docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文