param丢失或值为空: - RSpec

时间:2016-06-21 16:01:25

标签: ruby-on-rails ruby rspec

我已经彻底研究过这个问题,但我似乎无法找到答案。我正在编写一个模型规范,一切似乎都很常规,但我一直收到这个错误 - param is missing or the value is empty: subscriber - 我真的不知道还能说什么?我会发布我的代码和错误,以便更清晰。

CONTROLLER:

 def subscriber_params
  params.require(:subscriber).permit(:first_name, :last_name, :email, :phone_number)
 end 

SPEC:

 require "rails_helper"

 describe SubscribersController do
 include Devise::TestHelpers

 let(:user) { FactoryGirl.create(:user) }
 let(:subscriber) { FactoryGirl.create(:subscriber) }

 it "creates a new comment" do
  sign_in(user)
  comment = FactoryGirl.attributes_for(:comment)

  expect { post :create, comment: comment }.to change(Comment, :count).by(1)
  end
 end

ERROR:

enter image description here

如果您需要查看其他内容,请与我们联系。谢谢!

1 个答案:

答案 0 :(得分:4)

在您的强力参数中,您需要订阅者,但您没有根据您的请求发送订阅者。请尝试以下方法:

  require "rails_helper"

 describe SubscribersController do
 include Devise::TestHelpers

 let(:user) { FactoryGirl.create(:user) }
 let(:subscriber) { FactoryGirl.create(:subscriber) }

 it "creates a new comment" do
  sign_in(user)
  comment = FactoryGirl.attributes_for(:comment)

  expect { post :create, subscriber: subscriber, comment: comment }.to change(Comment, :count).by(1)
  end
 end