【问题标题】:Why are my tests telling me "param is missing or the value is empty:"?为什么我的测试告诉我“参数丢失或值为空:”?
【发布时间】: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


    【解决方案1】:

    我相信您只需要您的 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:

    【讨论】:

    • 这实际上是我在更改代码之前所拥有的。当我尝试这个时,我收到错误消息:Failure/Error: expect { count should have changed by 1, but was changed by 0
    • 在这种情况下,您的预约模型可能无效。您可以检查您的 Rspec 日志(查看它是否在尝试插入约会时回滚)或使用 Pry(和 binding.pry)进行调试,或者在控制器中使用一些“老派”puts 语句( puts @appointment.valid? 和/或 puts @appointment.errors.inspect 以查看模型的情况。
    • 你说得对。约会和服务之间的关联是我最近添加的,我没有更新我的约会控制器。
    • 甜蜜。很高兴你把它们弄绿了:)!
    【解决方案2】:

    您是否在控制器中使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多