1

私の Rails アプリでは、ユーザーの認証に devise を使用しています。ユーザーに属しているコントローラ Art の rspec テストを作成する必要があります。

私のアートモデルは次のとおりです。

class Art < ActiveRecord::Base
  belongs_to :user
  attr_accessible :description, :title, :image
  has_attached_file :image, :styles => { :medium => "620x620>", :thumb => "200x200>" }

  validates :title,       :presence => true
  validates :description, :presence => true
  validates :image,       :presence => true
end

私の ArtsController には、次のコードがあります。

class ArtsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @user = current_user
    @art = @user.arts.create(params[:art])
  end
end

アートを作成しようとしてログインしていないときに、ユーザーがサインインページにリダイレクトされるかどうかを確認するテストを作成しようとしています。したがって、私のテストは次のようになります。

describe ArtsController do
  describe "When user is not logged in" do
    it "should be redirected to sign in page if creating new art" do
      post :create
      response.should redirect_to '/users/sign_in'
    end
  end    
end

しかし、次のエラーが表示されます。

  1) ArtsController When user is not logged in should be redirected to sign in page if creating new art
     Failure/Error: post :create
     ActionController::RoutingError:
       No route matches {:controller=>"arts", :action=>"create"}
     # ./spec/controllers/arts_controller_spec.rb:11:in `block (3 levels) in <top (required)>'

私のroutes.rbは次のようなものです:

Capuccino::Application.routes.draw do
  devise_for :users
  match "home" => "users#home", :as => :user_home
  resources :users do
    resources :arts
  end
  match "home/art/:id" => "arts#show", :as => :art
  match "home/arts" => "arts#index", :as => :arts
end

このテストを実行するには、rspec テストをどのように行う必要がありますか?

4

1 に答える 1

0

arts#createコントローラーとアクションのみをパラメーターとして受け取るルートはありません。

:user_id現在のネストされたルートを使用する場合は、リクエストでパラメーターを渡す必要があります。

it "should be redirected to sign in page if creating new art" do
  post :create, { :user_id => user_id }
  response.should redirect_to '/users/sign_in'
end

しかし、 がないユースケースをテストしようとしているので、そのために新しいネストされていない:user_idルートが必要です。

于 2013-10-02T03:05:32.710 に答える