【问题标题】:Code Runs in Console but not with RSpec代码在控制台中运行,但不使用 RSpec
【发布时间】:2015-04-20 18:37:51
【问题描述】:

我有一个描述患者(数据包)的模型,该模型引用了目的地医院和运送救护车:

class Packet < ActiveRecord::Base
 belongs_to :hospital
 belongs_to :provider
 validates_presence_of :hospital_id
 validates_presence_of :provider_id
end 

class Hospital < ActiveRecord::Base
 has_many :packets
end

class Provider < ActiveRecord::Base
 has_many :packets
end

还有我的 RSpec 规范:

require "rails_helper"

RSpec.describe Packet, :type => :model do
  it "creates a new packet" do
    hospital = Hospital.create(:name=>"Community Hospital")
    provider = Provider.create(:name=>"Community Ambulance", :unit=>"Ambulance 3")

    packet = Packet.new()
    packet.hospital = hospital
    packet.provider = provider
    packet.save
    end
  end

RSpec 失败: 1) 包创建一个新包 失败/错误:packet.hospital = 医院 ActiveModel::MissingAttributeError: 无法写入未知属性hospital_id

我不明白的是,我的测试内容(“it”块中的所有内容)在 rails 控制台中运行良好,没有错误。为什么我会在 rspec 测试中得到未知属性,但在控制台中却没有?

完整的堆栈跟踪: Garys-MacBook-Air-2:EMSPacket gary$ rspec F

Failures:

1) Packet creates a new packet
    Failure/Error: packet.hospital = hospital
    ActiveModel::MissingAttributeError:
    can't write unknown attribute `hospital_id`
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/attribute.rb:124:in `with_value_from_database'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/attribute_set.rb:39:in `write_from_user'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/attribute_methods/write.rb:74:in `write_attribute_with_type_cast'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/attribute_methods/write.rb:56:in `write_attribute'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:92:in `write_attribute'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/attribute_methods.rb:373:in `[]='
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/associations/belongs_to_association.rb:80:in `replace_keys'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/associations/singular_association.rb:17:in `writer'
# /Users/gary/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:123:in `hospital='
# ./spec/models/packet_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.14495 seconds (files took 7.49 seconds to load)

1 个示例,1 个失败

Failed examples:

rspec ./spec/models/packet_spec.rb:4 # Packet creates a new packet

【问题讨论】:

  • 粘贴错误的完整堆栈跟踪。
  • 尝试在控制台中运行相同的代码,但使用测试环境: "RAILS_ENV=test rails console" 。如果这失败了,那么你没有迁移你的数据库。 “耙分贝:测试:准备”
  • 豪尔赫,谢谢。这是测试数据库

标签: ruby-on-rails activerecord rspec


【解决方案1】:

RSpec 正在尝试测试 it 块内的每一步,这就是测试失败但控制台工作的原因。您必须在测试之前创建具有属性和关系的记录,然后进行测试。

您为测试粘贴的代码实际上并未测试任何内容。

尝试测试可能真正失败的东西,例如保存错误或创建没有关联。但不要重复测试中的步骤。

describe "When creating new package" do
  let(:hospital) {Hospital.create(attributes)}
  let(:provider) {Provider.create(attributes)}
  let(:packet) {Packet.create(hospital_id: hospital.id, provider_id: provider.id)}

  it "should have the associations linked" do
    expect(package.hospital_id).to eq(hospital.id)
    expect(package.provider_id).to eq(provider.id)
  end
end

已编辑:

记得为测试数据库运行迁移:

rake db:test:prepare

【讨论】:

  • 但是我正在创建带有属性的记录。我和你一样使用 Hospital.create() 。我知道我的代码实际上并没有测试任何东西。我去掉了所有的 expect() 代码。我确实粘贴了您的代码(进行了一些小的更正),但我仍然得到“Packet 的未知属性 'hospital_id'”。
  • 在建议后用正确答案编辑。
【解决方案2】:

我喜欢在我的测试中使用https://github.com/thoughtbot/shoulda-matchers

应该是

describe 'associations' do
  it { should belong_to :hospital }
  it { should belong_to :provider }
end


describe 'validations' do
  it { should validate_presence_of :hospital_id }
  it { should validate_presence_of :provider_id }
end

不管怎样,看看https://github.com/thoughtbot/factory_girl,它也会帮助你进行测试。

【讨论】:

    【解决方案3】:

    感谢 Jorge de los Santos,问题在于我的测试数据库的设置。谢谢豪尔赫!

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多