0

認証にDeviseを使用するRailsアプリの簡単なマクロを生成しようとしています。基本的に、ユーザーが認証を必要とするページにアクセスしたときに、ログインページにリダイレクトされるようにしたいのです。だからこのようなもの:

it_requires_authentication_for :index, :new, :create, :update

ここでの望ましい結果は明らかです。ただし、私の問題は、各アクションを適切なhttpメソッド(:get、:postなど)にマップするための最良の方法を考えることができないことです。

私はこれから始めました:

def it_should_require_authentication_for(*actions)
  actions.each do |action|
    it "should require authentication for #{action}" do
      get action.to_sym
      response.should redirect_to( new_user_session_path )
    end
  end
end

もちろん、どちらが取得するだけです。誰かが私がすべてのアクションにこのマクロを提供する方法を教えてもらえますか?アクションが特定のメソッドに対して適切にルーティングされるかどうかを何らかの方法でテストする必要があると思いますが、実際にはよくわかりません。

どんな助けでも大歓迎です。

4

3 に答える 3

1

多分古いかもしれませんが、まだいくつかを助けることができます。

これは、RSpec でマクロを定義する (コントローラーの場合でも) 便利で簡単な方法です。

http://osmose.6spot.com.br/2011/02/better-macros-with-rspec/

ご覧ください。メソッド Missing を使用すると、記録したマクロに特定の動作を記録できます。たとえば、これは特定のスタブ命令を含む scaffold コントローラーの仕様です。

CustomersController を記述する

  before(:each) する
    mock_filter(:require_user_owner)
  終わり

  # GET /顧客
  get :index do
    デフォルト :stub => :off

    before(:each) する
      Customer.stub(:find_all_by_user_id) { [mock_customer] }
    終わり
  終わり

  # GET /顧客/6
  get :show, :id => 6

  # GET /顧客/新規
  取得:新規

  # GET /顧客/6/編集
  get :edit, :id => 6

  # POST /顧客
  投稿:作成

  # PUT /customers/6
  put :update, :id => 6

  # 削除 /customers/6
  削除 :destroy, :id => 6

終わり
于 2011-02-07T21:28:12.157 に答える
0

この Railscast はまさにあなたが探しているものをカバーしています: http://railscasts.com/episodes/157-rspec-matchers-macros

コントローラー仕様で目的のアクションを指定して get メソッドを呼び出すだけで、コントローラーで適切なメソッドが呼び出されるようです。

私のバージョンのマクロは次のとおりです。

# Last argument is an optional hash of http arguments
# which is useful when working with nested models
#
# ex it_should_require_login_for_actions(:index,:new,:create, {:parent_id=>1})
#
def it_should_require_login_for_actions(*actions)
  request_args = {:id => 1}

  #If the last element of the actions list is a hash, then merge it
  # with the existing request arguments
  if actions[-1].is_a?(Hash)
    request_args.merge!(actions.pop())
  end

  actions.each do |action|
    it "#{action} action should require login" do
      get action, request_args
      response.should redirect_to(login_url)
    end
  end
end
于 2011-10-10T21:25:42.067 に答える
0

もっとエレガントなものを思い付くまで、私は以下を使用しています:

# controller_macros.rb
def it_should_recognize_and_generate_routes_for(controller, routes)
  describe "routing" do
    routes.each do |route|
      action   = route[:action].to_s
      method   = route[:method] || :get
      url      = controller + (route[:url] || '')
      params   = route.reject {|k, v| [:action, :method, :url].include?(k) }
      expected = { :controller => controller, :action => action }.merge(params)

      it "should recognize and generate '#{action}'" do
        { method => url }.should route_to(expected)
      end
    end
  end
end

# posts_controller_spec.rb
describe Forum::PostsController do
  it_should_recognize_and_generate_routes_for('forum/posts', [
    { :action => :new, :url => '/new' },
    { :action => :create, :method => :post },
    { :action => :show, :url => '/1', :id => '1' },
    { :action => :index },
    { :action => :edit, :url => '/1/edit', :id => '1' },
    { :action => :update, :method => :put, :url => '/1', :id => '1' },
    { :action => :destroy, :method => :delete, :url => '/1', :id => '1' }
  ])
end

ところで、次のようなルートで動作するように拡張する必要があります。

get 'login' => 'user_sessions#new'
于 2010-10-23T06:15:18.770 に答える