Rspec, et CanCan Devise
-
25-09-2019 - |
Question
Je commence un projet et je voudrais pouvoir tout tester:)
Et j'ai quelques problèmes avec CanCan médite.
Pour exemple, j'ai les contacts contrôleur. Tout le monde peut voir et tout le monde (personnes interdites excepts) peuvent créer des contacts.
#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
Les travaux de code, mais je ne sais pas comment tester le contrôleur. J'ai essayé. Cela fonctionne si je commente la ligne load_and_authorize_resource.
#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
Mais ces tests totalement échoué .... Je ne voyais rien sur le web ... :( Donc, si vous pouvez me conseiller sur la façon dont je dois suivre, je serais heureux de vous l'oreille:)
La solution
Cancan ne remet pas Contact.new(params[:contact])
. Au contraire, il appelle contact.attributes = params[:contact]
plus tard, après avoir appliqué quelques attributs initiaux en fonction des autorisations de la capacité actuelle.
Voir numéro 176 pour plus de détails sur ce sujet et une solution de rechange. Je prévois d'obtenir ce corrigé dans la version 1.5 CanCan sinon plus tôt.