Rspec、CanCan 和 Devise [英] Rspec, CanCan and Devise
问题描述
我正在开始一个项目,我希望能够测试一切:)
I am starting a project and i would like to be able to test everything :)
我在 CanCan 和设计方面遇到了一些问题.
And i have some problems with CanCan and devise.
例如,我有一个控制器联系人.每个人都可以查看,每个人(被禁止的人除外)都可以创建联系人.
For exemple, I have a controller Contacts. Everybody can view and everybody (excepts banned people) can create contact.
#app/controllers/contacts_controller.rb
class ContactsController < ApplicationController
load_and_authorize_resource
def index
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
if @contact.save
respond_to do |f|
f.html { redirect_to root_path, :notice => 'Thanks'}
end
else
respond_to do |f|
f.html { render :action => :index }
end
end
end
end
代码有效,但我不知道如何测试控制器.我试过这个.如果我评论 load_and_authorize_resource 行,这会起作用.
The code work, but I don't how to test the controller. I tried this. This works if I comment the load_and_authorize_resource line.
#spec/controllers/contacts_controller_spec.rb
require 'spec_helper'
describe ContactsController do
def mock_contact(stubs={})
(@mock_ak_config ||= mock_model(Contact).as_null_object).tap do |contact|
contact.stub(stubs) unless stubs.empty?
end
end
before (:each) do
# @user = Factory.create(:user)
# sign_in @user
# @ability = Ability.new(@user)
@ability = Object.new
@ability.extend(CanCan::Ability)
@controller.stubs(:current_ability).returns(@ability)
end
describe "GET index" do
it "assigns a new contact as @contact" do
@ability.can :read, Contact
Contact.stub(:new) { mock_contact }
get :index
assigns(:contact).should be(mock_contact)
end
end
describe "POST create" do
describe "with valid params" do
it "assigns a newly created contact as @contact" do
@ability.can :create, Contact
Contact.stub(:new).with({'these' => 'params'}) { mock_contact(:save => true) }
post :create, :contact => {'these' => 'params'}
assigns(:contact).should be(mock_contact)
end
it "redirects to the index of contacts" do
@ability.can :create, Contact
Contact.stub(:new) { mock_contact(:save => true) }
post :create, :contact => {}
response.should redirect_to(root_url)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved contact as @contact" do
@ability.can :create, Contact
Contact.stub(:new).with({'these' => 'params'}) { mock_contact(:save => false) }
post :create, :contact => {'these' => 'params'}
assigns(:contact).should be(mock_contact)
end
it "re-renders the 'new' template" do
@ability.can :create, Contact
Contact.stub(:new) { mock_contact(:save => false) }
post :create, :contact => {}
response.should render_template("index")
end
end
end
end
但是这些测试完全失败了......我在网上什么也没看到...... :(所以,如果你能在我必须遵循的方式上给我建议,我会很高兴听到你的声音:)
But these tests totally failed .... I saw nothing on the web ... :( So, if you can advise me on the way i have to follow, i would be glad to ear you :)
推荐答案
CanCan 不调用 Contact.new(params[:contact])
.相反,它会在根据当前的能力权限应用一些初始属性后稍后调用 contact.attributes = params[:contact]
.
CanCan does not call Contact.new(params[:contact])
. Instead it calls contact.attributes = params[:contact]
later after it has applied some initial attributes based on the current ability permissions.
请参阅问题 #176 了解有关此问题和替代解决方案的详细信息.我计划尽快在 CanCan 1.5 版中修复此问题.
See Issue #176 for details on this and an alternative solution. I plan to get this fixed in CanCan version 1.5 if not sooner.
这篇关于Rspec、CanCan 和 Devise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!