代码在控制台中运行,但不在RSpec中运行

时间:2015-04-20 18:37:52

标签: ruby-on-rails activerecord rspec

我有一个模型描述了一个病人(一个包),它引用了目的地医院和运输救护车:

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 = hospital       ::加载ActiveModel MissingAttributeError:         无法写出未知属性hospital_id

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

完整堆栈跟踪:     Garys-MacBook-Air-2:EMSPacket加里$ 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

3 个答案:

答案 0 :(得分:1)

RSpec正试图测试它内部的everystep,这就是为什么测试失败但控制台工作的原因。在测试之前,你必须使用属性和关系创建记录,然后测试一些东西。

您为测试ID粘贴的代码实际上没有测试任何内容。

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

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

答案 1 :(得分:0)

我喜欢在我的测试中使用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,它也会帮助你进行测试。

答案 2 :(得分:0)

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