[rspec-users] Another "how do I spec this?"

Pat Maddox pergesu at gmail.com
Mon Jan 1 04:05:02 EST 2007


I wanted to add a convenience method on my User class to see if he was
already signed up for a tournament.  Here's my spec

context "A User signed up for one tournament" do
  setup do
    @t1 = Tournament.new
    @t1.save false
    @t2 = Tournament.new
    @t2.save false
    @user = User.new
    @user.save false
    @user.registrations << Registration.new(:tournament => @t1)
  end

  specify "should be signed up for that tournament" do
    @user.should_be_signed_up_for @t1
  end

  specify "should not be signed up for another tournament" do
    @user.should_not_be_signed_up_for @t2
  end
end

class User < ActiveRecord::Base
  has_many :registrations

  def signed_up_for?(tournament)
    !registrations.find_by_tournament_id(tournament).nil?
  end
end

Are there any potential improvements, or is that the best way to spec it?

Pat


More information about the rspec-users mailing list