控制器规格&强大的参数

时间:2016-02-11 01:57:53

标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails strong-parameters

非常基本的控制器规格失败,因为设置了强参数的东西很难?

class OrdersController
  def create
    @order = Order.new(order_params)
    if @order.valid?
      ...
  end

  private
  def order_params
    params.require(:order).and_permit(:email)
  end
end

测试代码:

describe OrdersController, "Create action", type: :controller do
  it "should call valid? method" do
    Order.any_instance.should_receive(:valid?)
    post :create, order: {email: "test@example.com"}
  end
end

结果:

Failure/Error: post :create, order: {email: "test@example.com"}
 NoMethodError:
   undefined method `and_permit' for {"email"=>"test@example.com"}:ActionController::Parameters

1 个答案:

答案 0 :(得分:0)

jvnill对原帖的评论击中了头部,permit and_permit

params.require(:order).permit(:email)