2013-08-21 36 views
0

我目前在西納特拉/ DataMapper的rspec/datamapper - 如何告訴rspec期望出現錯誤?

class Score 
include DataMapper::Resource 

property :score, Integer 

property :created_at, DateTime, :default => DateTime.now, :lazy => [:show] 
property :updated_at, DateTime, :default => DateTime.now, :lazy => [:show] 

belongs_to :pageant, :key => true 
belongs_to :candidate, :key => true 
belongs_to :category, :key => true 
belongs_to :judge, :key => true 

end 

測試該類這個RSpec的測試

it 'that can be inserted by a judge if a pageant is active' do 
     score_count = Score.all.length 
     post '/score', @correct_score_data 
     Score.all.length.should eq score_count+1 
    end 

    it 'cannot be duplicated if it has been sent' do 
     score_count = Score.all.length 
     post '/score', @correct_score_data 
     Score.all.length.should eq score_count 
    end 

基本上什麼是應該發生的是,法官只能發送一個得分特定類別+候選人+選美組合一次,之後我想否認下一個分數。現在,當我運行這個時,我得到一個IntegrityError(我期望)。我如何告訴rspec我「期望看到這個錯誤」?你們也可以批評我的代碼,我仍然在學習所有這些一起

+1

這將是有益的:http://stackoverflow.com/questions/1722749/how-to-use-rspecs-should-raise-with-any-kind-例外 – cortex

回答

0

使用expect{}.to raise_errorhttps://www.relishapp.com/rspec/rspec-expectations/v/2-6/docs/built-in-matchers/raise-error-matcher

我不完全理解你的規格(它看起來像你的應用程序狀態是由兩個測試之間泄漏),但這樣的事情...

it 'cannot be duplicated if it has been sent' do 
    score_count = Score.all.length 
    expect { post '/score', @correct_score_data }.to raise_error(IntegrityError) 
end 
+0

試過了,現在是有效的。這是什麼意思應用程序狀態在兩次測試之間泄漏? –

+0

嗯,你發佈的兩個規格看起來像是做了完全相同的事情,但期望得到不同的結果。除非有更多信息沒有發佈,否則我認爲您期望一個測試運行,成功,然後運行下一個測試,並將應用程序推入「不良」狀態。兩個規格不應該要求對方工作。如果那不是那種情況,那麼我只是假設太多了! –

+0

啊是啊應該發生的是發送一個,它的工作原理,發送相同的東西,它不工作......猜我需要閱讀更多 –

相關問題