我目前在西納特拉/ 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我「期望看到這個錯誤」?你們也可以批評我的代碼,我仍然在學習所有這些一起
這將是有益的:http://stackoverflow.com/questions/1722749/how-to-use-rspecs-should-raise-with-any-kind-例外 – cortex