2012-03-04 6 views
1

テスト中にput/delete/postなどを直接呼び出すと、RSpecテストに合格するのに問題があります。私はそれがput/delete等のリクエストの直接呼び出しでセッションが渡されなかった結果だと信じていますが、私は100%確実ではありません。RSpec機能テストでログインできません

さらに情報:これは、レールのチュートリアルからのものであり、私が代わりに、第8章(http://ruby.railstutorial.org/chapters/sign-in-sign-out?version=3.2#sec:sign_in_out_exercises)でクッキーのセッションを使用するように運動を実施し

このテストパス(だから私は、セッションがで正しく動作しているかなり確信していますブラウザ):

describe "signin" do 
    before { visit signin_path } 

    describe "with valid information" do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
     fill_in "Email", with: user.email 
     fill_in "Password", with: user.password 
     click_button "Sign in" 
    end 

    it { should have_selector('title', text: user.name) } 
    it { should have_link('Users', href: users_path) } 
    it { should have_link('Profile', href: user_path(user)) } 
    it { should have_link('Settings', href: edit_user_path(user)) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should_not have_link('Sign in', href: signin_path) } 
    describe "followed by signout" do 
     before { click_link "Sign out" } 
     it { should have_link('Sign in') } 
    end 
    end 
end 

この1つはしない(ライン前{削除USER_PATH(ユーザ)}が失敗した):

describe "Authentication" do 
    describe "authorization" do 
     describe "as non-admin user" do 
      let(:user) { FactoryGirl.create(:user) } 
      let(:non_admin) { FactoryGirl.create(:user) } 
      before { sign_in non_admin } 
      describe "submitting a DELETE request to the Users#destroy action" do 
       before { delete user_path(user) } 
       specify { response.should redirect_to(root_path) } 
      end 
     end 
    end 
end 

エラーは次のとおりです。

Authentication authorization as non-admin user submitting a DELETE request to the Users#destroy action 
Failure/Error: before { delete user_path(user) } 
NoMethodError: 
    undefined method `admin?' for nil:NilClass 
    - ./app/controllers/users_controller.rb:55:in `admin_user' 
    - ./spec/requests/authentication_pages_spec.rb:133:in `block (5 levels) in <top (required)>' 

ユーザーコントローラとセッションヘルパーの関連ビット:

class UsersController < ApplicationController 
before_filter :admin_user,  only: :destroy 

def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User destroyed." 
    redirect_to users_path 
end 

private 

    def admin_user 
    redirect_to(root_path) unless current_user.admin? 
    end 

end 

module SessionsHelper 
    def sign_in(user) 
    #cookies.permanent[:remember_token] = user.remember_token 
    session[:user_id] = user.id 
    self.current_user= user 
    end 
    def sign_out 
    #cookies.delete(:remember_token) 
    session[:user_id] = nil 
    self.current_user= nil 
    end 

    def signed_in? 
    !current_user.nil? 
    #!session[:user_id].nil? 
    end 

    def current_user=(user) 
    @current_user = user 
    end 

    def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    end 
    def current_user?(user) 
    user == current_user 
    end 

    def redirect_back_or(default) 
    redirect_to(session[:return_to] || default) 
    clear_return_to 
    end 

    def store_location 
    session[:return_to] = request.fullpath 
    end 
    def clear_return_to 
    session.delete(:return_to) 
    end 

end 
+0

明らかに 'sign_in'呼び出しがうまくいかなかったので、あなたの' current_user'はまだnilです。 'before {sign_in non_admin; puts current_user.inspect} 'を実行し、何が起こるかを確認します。 – lulalala

答えて

0

は、あなたがそのような要求仕様のためにセッション変数を設定することができますか?私は、ログインにナビゲートする関数を定義し、フォームを埋めて、ログインボタンをクリックします。ログインしたユーザーが必要なスペックより前に呼びます。私は要求仕様に比較的新しいですが、それを行うための他の方法があるかもしれません。

+0

いいえ、わかりません。まだRoR/RSpecを学んでいるので、これをどのように実装すべきかについていくつかのガイダンスを探しています。 – McGin

関連する問題