在rspec测试中缺少Param

时间:2016-08-14 10:05:19

标签: ruby-on-rails ruby rspec

我定义了以下RSpec测试:

test 'invalid signup information' do
    get signup_path
    assert_no_difference 'User.count' do
        post users_path, params: { user: { name:  '',
                                           email: 'user@invalid',
                                           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

如果我执行rake测试,我会收到以下错误:

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 个答案:

答案 0 :(得分:0)

我不是,如果这是正确的,但在我看来你是创建了一个intregation测试,但想要一个控制器。

参见此示例

mount -o username=guest,password=guest,file_mode=0777,dir_mode=0777 -t cifs //192.168.1.254/public /storage/nas/public

more info for integration test