2016-08-14 3 views
0

に欠落しています。Paramのは、私は以下のRSpecのテストが定義されているRSpecのテスト

test 'invalid signup information' do 
    get signup_path 
    assert_no_difference 'User.count' do 
     post users_path, params: { user: { name: '', 
              email: '[email protected]', 
              password:    'foo', 
              password_confirmation: 'bar' } } 
    end 
    assert_template 'users/new' 
end 

そして、次のコントローラー:

class UsersController < ApplicationController 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     redirect_to new_user_path 
    else 
     render :new 
    end 
    end 

    def show 
    @user = User.find(params[:id]) 
    end 

    private 

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

私は熊手テストを実行すると、私は次のエラーを取得:

ERROR["test_invalid_signup_information", UserSignupTest, 0.35556070700022246] 
test_invalid_signup_information#UserSignupTest (0.36s) 
ActionController::ParameterMissing:   ActionController::ParameterMissing: param is missing or the value is empty: user 
      app/controllers/users_controller.rb:23:in `user_params' 
      app/controllers/users_controller.rb:8:in `create' 
      test/integration/user_signup_test.rb:7:in `block (2 levels) in <class:UserSignupTest>' 
      test/integration/user_signup_test.rb:6:in `block in <class:UserSignupTest>' 

user_paramsのrequireステートメントを削除すると問題なくテストが実行されます。しかし、私はユーザーを送る - それはなぜ失敗するのですか?

+1

この例を参照してください 'paramsは使用しないでください:'テスト中。 '{user:{..}}'ハッシュを渡すだけです。 –

+0

ありがとうございます。私は古いレールのスタイルを使用しましたか?私はチュートリアルをちょうど終えていたので、 – CamelWriter

+0

いいえ、考えられません。私の提案は有効ですか? –

答えて

0

私はこれが正しいとは思っていませんが、私の意見では、あなたは照合テストを作成しましたが、コントローラにしたいです。

# spec/controllers/contacts_controller_spec.rb 

# rest of spec omitted ... 

describe "POST create" do 
    context "with valid attributes" do 
    it "creates a new contact" do 
     expect{ 
     post :create, contact: {name: 'test'} 
     }.to change(Contact,:count).by(1) 
    end 

more info for integration test

関連する問題