0

私はレールを使ったテストに非常に慣れていないため、テストの世界で迷子になっているようです。現在、ダッシュボード コントローラーをテストしており、コントローラーからload_and_authorize_resource行を削除するとすべてがパスします。認証に cancan を使用しています。

dashboard_controller.rb

def update
@dashboard = Dashboard.find(params[:id])

respond_to do |format|
  if @dashboard.update_attributes(params[:dashboard])
    format.html { redirect_to dashboards_path, notice: 'dashboard was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @dashboard.errors, status: :unprocessable_entity }
  end
end
end

dashboard_controller_spec.rb

require 'spec_helper'

describe DashboardsController do

  login_user
  before :each do 
    @dashboard = create(:dashboard, dashboard_name: "My Own Dashboard", id: 1)
  end

  describe "GET #index" do

    it "should have a current_user" do 
      subject.current_user.should_not be_nil
    end

    it "renders the :index view" do
      get :index
      response.should render_template :index
    end

    it "Creates new dashboard" do 
      get :new
      response.should render_template :new
    end

  end

  describe "Get #edit" do 

    it "assigns dashboard to @dashboard" do 
      get :edit, id: @dashboard
      assigns(:dashboard).should == @dashboard
    end

    it "renders the :edit template" do 
      get :edit, id: @dashboard
      response.should render_template :edit
    end

  end


end

コンソールから受け取るエラー

 1) DashboardsController Get #edit renders the :edit template
 Failure/Error: response.should render_template :edit
   expecting <"edit"> but rendering with <"">
 # ./spec/controllers/dashboard_controller_spec.rb:37:in `block (3 levels) in <top (required)>'

とにかく、dashboard_controller の load_and_authorize_resource を削除せずにこのエラーを回避するには?

4

1 に答える 1

0

Devise が関係している場合のコントローラーのテストは、厄介なほどトリッキーです。login_user コードを提供していませんが、すべてのベースをカバーしていないと思います。Deviseの公式ドキュメントに従って、warden、Devise のユーティリティ メソッドなどを支援する必要があります。ここで繰り返しますが、少し長くなります。あなたもソースにアクセスしてください。最も適切な部分は次のとおりです。

デバイスのユーティリティメソッドのいずれかを使用している場合、コントローラーの仕様はそのままでは機能しません。

rspec-rails-2.0.0 および devise-1.1 の時点で、devise を仕様に組み込む最良の方法は、単純に次を spec_helper に追加することです...

次に、サンプルの controller_macros.rb を追加し、他のものを微調整すると、準備完了です。ただし、コントローラの仕様のテストはリクエストの仕様のテストとは異なるため、このソリューションでは対処できない別のシナリオでハングアップする可能性があることに注意してください ( https://stackoverflow.com/で私自身の苦痛と発見の一部を参照してください)。 a/13275366/9344 )。

于 2012-11-28T10:35:42.893 に答える