Question

I'm currently testing this class in 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

with this rspec test

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

basically what is supposed to happen is that a judge can only send a score for a specific category+candidate+pageant combination once, after which I'm suppose to deny the next scores. Now when I run this I get an IntegrityError (which I expect). How do I tell rspec that I "expect to see this error"? You guys can also critique my code, I'm still learning all of these together

Was it helpful?

Solution

Use expect{}.to raise_error: https://www.relishapp.com/rspec/rspec-expectations/v/2-6/docs/built-in-matchers/raise-error-matcher

I don't fully understand your specs (it looks like your application state is leaking between the two tests), but something like this...

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top