Rspec测试失败了强大的参数

时间:2017-11-15 00:53:08

标签: ruby-on-rails ruby ruby-on-rails-3 rspec rspec-rails

尝试让这一个通过一段时间,不熟悉Rspec,尝试了几个答案,但没有运气,我的控制器需要这两个参数如下:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_back fallback_location: new_user_path
    else
      render :new
    end
  end
private
  def user_params
    params.require(:user).permit(:username, :password)
  end
end

这是给我一个问题的rspec测试:

  describe "GET #create" do
    it "returns http success" do
      get :create
      expect(response).to have_http_status(:success)
    end
  end

这是我得到的错误:

1) UsersController GET #create returns http success
     Failure/Error: params.require(:user).permit(:username, :password)

     ActionController::ParameterMissing:
       param is missing or the value is empty: user

我现在正在查看rspec文档,但任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:4)

试试这个:

describe "POST #create" do
  it "returns http success" do
    post :create, params: { user: { username: "name", password: "password" } }
    expect(response).to have_http_status(:success)
  end
end

应该是post :create,而不是get :create