Frage

Ich bin ein Projekt beginnen, und ich möchte zu testen alles zu:)

Und ich habe einige Probleme mit Cancan und devise.

Für exemple, habe ich einen Controller Kontakt. Jeder kann sehen, und jeder (excepts verboten Personen) können Kontakt erstellen.

#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

Der Code funktioniert, aber ich weiß nicht, wie die Steuerung zu testen. Ich versuchte dies. Dies funktioniert, wenn ich die load_and_authorize_resource Linie Stellung zu nehmen.

#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

Aber diese Tests völlig versagt .... Ich sah nichts im Web ... :( Also, wenn Sie mich auf dem Weg raten kann ich folgen muss, würde ich zu Ohr Ihnen gerne:)

War es hilfreich?

Lösung

CanCan nicht Contact.new(params[:contact]) nennen. Stattdessen ruft es contact.attributes = params[:contact] später, nachdem es einige erste Attribute auf Basis der aktuellen Fähigkeit Berechtigungen angewendet hat.

Siehe Ausgabe # 176 Einzelheiten hierzu und eine alternative Lösung. Ich plane dies in CanCan Version 1.5 festgelegt werden, wenn nicht früher.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top