【问题标题】:Rspec, CanCan and DeviseRspec、CanCan 和设计
【发布时间】:2010-11-17 00:07:47
【问题描述】:

我正在开始一个项目,我希望能够测试所有内容:)

我在 CanCan 和设计方面遇到了一些问题。

例如,我有一个控制器联系人。每个人都可以查看,每个人(被禁止的人除外)都可以建立联系。

#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

但是这些测试完全失败了...... 我在网上什么也没看到... :( 所以,如果你能就我必须遵循的方式给我建议,我会很高兴听到你的声音:)

【问题讨论】:

    标签: ruby-on-rails-3 rspec devise cancan


    【解决方案1】:

    CanCan 不调用Contact.new(params[:contact])。相反,它会在根据当前能力权限应用一些初始属性后调用contact.attributes = params[:contact]。

    请参阅Issue #176 了解有关此问题和替代解决方案的详细信息。我计划尽快在 CanCan 1.5 版中解决这个问题。

    【讨论】:

    • 谢谢瑞恩!我去看看!
    • 嗨 Ryan,我查看了您的链接,感谢 voxik,我应用了他的补丁,现在我能够通过所有测试。我希望你能尽快在新版本的 Cancan 上发布补丁。再次感谢!
    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多