我目前正在Sinatra / Datamapper中测试这个类
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我“希望看到这个错误”?你们也可以批评我的代码,我还在一起学习所有这些
答案 0 :(得分:0)
使用expect{}.to raise_error
:https://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