سؤال

أبدأ مشروعًا وأود أن أكون قادرًا على اختبار كل شيء :)

ولدي بعض المشكلات مع كانكان وترسم.

بالنسبة لـ exemple ، لدي جهات اتصال تحكم. يمكن للجميع المشاهدة ويمكن للجميع (باستثناء الأشخاص المحظورون) إنشاء اتصال.

#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.

#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

لكن هذه الاختبارات فشلت تمامًا .... لم أر شيئًا على الويب ... :( لذا ، إذا كنت تستطيع أن تنصحني بالطريقة التي يجب أن أتابعها ، سأكون سعيدًا بأذنك :)

هل كانت مفيدة؟

المحلول

كانكان لا يتصل Contact.new(params[:contact]). بدلا من ذلك يدعو contact.attributes = params[:contact] في وقت لاحق بعد أن قام بتطبيق بعض السمات الأولية بناءً على أذونات القدرة الحالية.

يرى العدد رقم 176 للحصول على تفاصيل حول هذا وحل بديل. أخطط للحصول على هذا ثابت في Cancan الإصدار 1.5 إن لم يكن عاجلاً.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top