I'm trying to write an rspec for my Rooms controller t开发者_JAVA百科o 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
That's not RSpec syntax, what you want is:
@controller.stub!(:current_ability).and_return(@ability)
Newer Version 3.8 allow
Syntax.
allow( @controller ).to receive( :current_ability ).and_return( @ability )
As per the Relish 3.8 docs.
精彩评论