【发布时间】:2015-08-18 21:35:10
【问题描述】:
使用 Rails 4.2、rspec 2.14、rspec-rails 2.14、faker 和 factory-girls-rails gems
我有一个名为 Appointment 的模型,我正在对它进行一些测试,除了控制器规范下的 #create 之外,一切都通过了。
我得到的错误信息是:
失败/错误:post :create, FactoryGirl.attributes_for(:appointment) ActionController::ParameterMissing: 参数缺失或值为空:约会
Appointment 模型验证是否存在与名为 Service 的对象的关联。
这是我预约的工厂.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
这是我的约会规格.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 列出的说明制作我的控制器规格。我还发现很多 stackoverflow 帖子都说要做同样的事情,但我仍然遇到同样的错误。
以下是我的约会控制器规格.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
我改变这个的原因是因为当我得到这个时,我原来的错误信息是:
失败/错误:期望 { count 应该改了 1,但是改了 0
很抱歉给您带来了困惑。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rspec factory-bot rspec-rails