0

我目前正在 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 我“希望看到这个错误”?你们也可以批评我的代码,我还在一起学习所有这些

4

1 回答 1

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
于 2013-08-22T15:09:56.937 回答