2015-04-20 23 views
0

我有一個描述病人(一包),它引用了目的地醫院和運送救護模型:代碼運行的控制檯,但不使用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: 無法寫未知att ribute hospital_id

我沒有得到的東西是我的測試的肉(「it」塊中的所有東西)在rails控制檯中運行良好,沒有錯誤。爲什麼我會在rspec測試中獲取未知屬性,但不在控制檯中?

完整的堆棧跟蹤: Garys-的MacBook空中-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 
+0

粘貼完整堆棧錯誤的跟蹤。 – tebayoso

+0

嘗試在控制檯中運行相同的代碼,但使用測試環境:「RAILS_ENV =測試導軌控制檯」。如果失敗,那麼你沒有遷移你的數據庫。 「rake db:test:prepare」 – tebayoso

+0

Jorge,謝謝。這是測試db – simusid

回答

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 
+0

但我正在創建具有屬性的記錄。我正在使用Hospital.create()。我知道我的代碼實際上沒有測試任何東西。我刪除了所有expect()代碼。我確實粘貼了你的代碼(有一些小的更正),我仍然得到「未知屬性'hospital_id'的數據包。」 – simusid

+0

在建議後用正確答案編輯。 – tebayoso

0

感謝Jorge de los Santos,問題在於我的測試數據庫的設置。感謝豪爾赫!

相關問題