非常基本的控制器规格失败,因为设置了强参数的东西很难?
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
答案 0 :(得分:0)
jvnill对原帖的评论击中了头部,permit
and_permit
params.require(:order).permit(:email)