Rspec,CanCan和Devise [英] Rspec, CanCan and Devise
问题描述
我正在开始一个项目,我想能够测试所有的东西:)
I am starting a project and i would like to be able to test everything :)
我有一些CanCan和devise的问题。
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.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屋!