6

ユーザーを作成しようとするか、新しいユーザー パスにアクセスしようとすると、既にサインインしているユーザーを root_path にリダイレクトするテストとアプリケーション コードを作成しようとしています。

user_pages_spec.rb に書いたテストは次のとおりです。

describe "for signed in users" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      describe "using a 'new' action" do
        before { get new_user_path }
        specify { response.should redirect_to(root_path) }
      end

      describe "using a 'create' action" do
        before { post users_path }
        specify { response.should redirect_to(root_path) }
      end         
  end 

ユーザーコントローラー:

class UsersController < ApplicationController
  before_action :unsigned_in_user, only: [:create, :new]

  def new
    @user = User.new
  end

  def create
     @user = User.new(user_params)
     if @user.save
      sign_in @user
          flash[:success] = "Welcome to the Sample App!"
          redirect_to @user
     else
          render 'new'
     end
  end

  private
    # Before filters

    def user_params
      params.require(:user).permit(:name, :email, :password,
                               :password_confirmation)
    end

    def unsigned_in_user
      puts signed_in?
      redirect_to root_url, notice: "You are already signed in." unless !signed_in?
    end
end

puts signed_in?false を返します。true が返されると予想されるため、これが問題であると想定しています。rspec を使用してテストを実行した後のエラーを次に示します。どんな助けでも大歓迎です。

Failures:

  1) User pages for signed in users using a 'create' action 
     Failure/Error: before { post users_path }
     ActionController::ParameterMissing:
       param not found: user
     # ./app/controllers/users_controller.rb:52:in `user_params'
     # ./app/controllers/users_controller.rb:20:in `create'
     # ./spec/requests/user_pages_spec.rb:162:in `block (4 levels) in <top (required)>'

  2) User pages for signed in users using a 'new' action 
     Failure/Error: specify { response.should redirect_to(root_path) }
       Expected response to be a <redirect>, but was <200>
     # ./spec/requests/user_pages_spec.rb:158:in `block (4 levels) in <top (required)>'

sessions_helper.rb ファイル内:

def signed_in?
    !current_user.nil?
end

spec/support/utilities.rb:

def sign_in(user, options={})
  if options[:no_capybara]
    # Sign in when not using Capybara.
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
  else
    visit signin_path
    fill_in "Email",    with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end
end
4

2 に答える 2

4

テストに合格することができましたか?

そうでない場合、私は今日あなたと同じ問題を抱えていましたが、テストに 2 つの変更を加えることでテストに合格することができました - userPOST 時にハッシュを渡すことと、メソッドでオプションを使用することno_capybaraです。同じテスト内でカピバラからカピバラ以外のメソッドに切り替えると、RSpec は期待どおりに動作しないと思います。sign_ingetpost

describe "for signed-in users" do
  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user, no_capybara: true }

  describe "using a 'new' action" do
    before { get new_user_path }      
    specify { response.should redirect_to(root_path) }
  end

  describe "using a 'create' action" do
    before do
      @user_new = {name: "Example User", 
                   email: "user@example.com", 
                   password: "foobar", 
                   password_confirmation: "foobar"} 
      post users_path, user: @user_new 
    end

    specify { response.should redirect_to(root_path) }
  end
end   
于 2013-08-07T07:00:27.993 に答える
3

najwa と同じ答えですが、重複を避けるために Rails属性メソッドでFactoryGirlユーザーを使用しました。

describe "using a 'create' action" do
  before { post users_path, user: user.attributes }

  specify { response.should redirect_to(root_path) }
end

データをテスト コードから切り離しておくのに役立ちます。

于 2013-09-07T22:41:11.290 に答える