为什么我的测试告诉我 "param is missing or the value is empty:"?
Why are my tests telling me "param is missing or the value is empty:"?
使用 Rails 4.2,rspec 2.14,rspec-rails 2.14,faker 和 factory-girls-rails gems
我有一个名为 Appointment 的模型,我正在对它进行 运行 一些测试,除了控制器规范下的 #create 之外,一切都通过了。
我收到的错误信息是:
Failure/Error: post :create, FactoryGirl.attributes_for(:appointment)
ActionController::ParameterMissing:
param is missing or the value is empty: appointment
约会模型验证是否存在与称为服务的对象的关联。
这是我的 appointment.rb 工厂:
require 'faker'
FactoryGirl.define do
factory :appointment do |f|
f.service {FactoryGirl.create(:service)}
f.appointment_time { Faker::Time.between(DateTime.now - 1, DateTime.now) }
end
end
这是我的 appointment_spec.rb:
require 'spec_helper'
describe Appointment do
it "has a valid factory" do
FactoryGirl.create(:appointment).should be_valid
end
it "is invalid if it does not have a Service association" do
FactoryGirl.build(
:appointment, service: nil).should_not be_valid
end
end
我一直在按照 here 列出的说明来制作我的控制器规范。我还发现很多 Whosebug 帖子都说要做同样的事情,但我仍然得到同样的错误。
这是我的 appointment_controller_spec.rb
未通过的测试
describe AppointmentsController do
#other controller action code...
describe "POST #create" do
context "with valid attributes" do
it "saves the new appointment in the database" do
expect {
post :create, FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
it "redirects to show page" do
post :create, FactoryGirl.attributes_for(:appointment)
response.should redirect_to Appointment.last
end
end
end
我很茫然,希望有人能提供一些见解。
编辑:
正如你们中的一些人所建议的,我更改了控制器规格。这实际上是我在将代码更改为您在上面看到的内容之前所拥有的:
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
我改变这个的原因是当我有这个时我原来的错误信息是:
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
抱歉造成混淆。
我相信您只需要按如下方式阅读 AppointmentsController 规范:
describe AppointmentsController do
#other controller action code...
describe "POST #create" do
context "with valid attributes" do
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
it "redirects to show page" do
post :create, appointment: FactoryGirl.attributes_for(:appointment)
response.should redirect_to Appointment.last
end
end
end
在 post
调用中通过 FactoryGirl 提供属性之前添加 appointment:
。
您是否在控制器中使用 strong_params?看起来您正在寻找约会参数,但您只是获得了属性的哈希值。
试试这个:
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
使用 Rails 4.2,rspec 2.14,rspec-rails 2.14,faker 和 factory-girls-rails gems
我有一个名为 Appointment 的模型,我正在对它进行 运行 一些测试,除了控制器规范下的 #create 之外,一切都通过了。
我收到的错误信息是:
Failure/Error: post :create, FactoryGirl.attributes_for(:appointment) ActionController::ParameterMissing: param is missing or the value is empty: appointment
约会模型验证是否存在与称为服务的对象的关联。
这是我的 appointment.rb 工厂:
require 'faker'
FactoryGirl.define do
factory :appointment do |f|
f.service {FactoryGirl.create(:service)}
f.appointment_time { Faker::Time.between(DateTime.now - 1, DateTime.now) }
end
end
这是我的 appointment_spec.rb:
require 'spec_helper'
describe Appointment do
it "has a valid factory" do
FactoryGirl.create(:appointment).should be_valid
end
it "is invalid if it does not have a Service association" do
FactoryGirl.build(
:appointment, service: nil).should_not be_valid
end
end
我一直在按照 here 列出的说明来制作我的控制器规范。我还发现很多 Whosebug 帖子都说要做同样的事情,但我仍然得到同样的错误。
这是我的 appointment_controller_spec.rb
未通过的测试describe AppointmentsController do
#other controller action code...
describe "POST #create" do
context "with valid attributes" do
it "saves the new appointment in the database" do
expect {
post :create, FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
it "redirects to show page" do
post :create, FactoryGirl.attributes_for(:appointment)
response.should redirect_to Appointment.last
end
end
end
我很茫然,希望有人能提供一些见解。
编辑:
正如你们中的一些人所建议的,我更改了控制器规格。这实际上是我在将代码更改为您在上面看到的内容之前所拥有的:
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
我改变这个的原因是当我有这个时我原来的错误信息是:
Failure/Error: expect { count should have been changed by 1, but was changed by 0
抱歉造成混淆。
我相信您只需要按如下方式阅读 AppointmentsController 规范:
describe AppointmentsController do
#other controller action code...
describe "POST #create" do
context "with valid attributes" do
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
it "redirects to show page" do
post :create, appointment: FactoryGirl.attributes_for(:appointment)
response.should redirect_to Appointment.last
end
end
end
在 post
调用中通过 FactoryGirl 提供属性之前添加 appointment:
。
您是否在控制器中使用 strong_params?看起来您正在寻找约会参数,但您只是获得了属性的哈希值。
试试这个:
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end