Skip to content

Instantly share code, notes, and snippets.

@mattm
Created November 26, 2011 01:23
Show Gist options
  • Select an option

  • Save mattm/1394796 to your computer and use it in GitHub Desktop.

Select an option

Save mattm/1394796 to your computer and use it in GitHub Desktop.
## Favorite.rb
class Favorite < ActiveRecord::Base
belongs_to :user
attr_accessible :name
validates_presence_of :name
validates_presence_of :user
validates_format_of :name, :with => /\A[a-z]+\Z/
before_create :ensure_unique_for_this_user
private
# Ensure the user only has one favorite by this name
def ensure_unique_for_this_user
user.favorites.find_by_name(name).nil?
end
end
## Favorites Test
require 'spec_helper'
describe Favorite do
it 'fails validation with invalid attributes' do
f = Favorite.new
f.should_not be_valid
f.errors.count.should eq(3)
f.errors[:user].should eq(["can't be blank"])
f.errors[:name].should eq(["can't be blank", "is invalid"])
f.user = Factory(:quentin)
f.name = "flowerspot"
f.should be_valid
end
it 'only allows lowercase letters' do
f = Favorite.new
['with space', 'with$pecial', 'withUpper'].each do |name|
f.name = name
f.should_not be_valid
f.errors[:name].should eq(["is invalid"])
end
end
end
@chrisconley
Copy link

I've become a fan of the context and let syntax in rspec.

require 'spec_helper'

describe Favorite do
  describe "validations" do
    describe "#user" do
      let(:favorite) { Factory(:favorite, :user => user) }
      subject { favorite.errors[:user] }
      context "when user is nil" do
        let(:user) { nil }
        it { should == ["can't be blank"] }
      end
      context "when user exists" do
        let(:user) { Factory(:user)}
        it { should == p[] }
      end
    end
  end
end

This is a decent post on rspec best practices:
http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

@nicholaides
Copy link

What Chris said. :)

@mattm
Copy link
Author

mattm commented Nov 27, 2011

Well, this kind of blew my mind. I didn't know about subject, context, or let. How did you guys learn RSpec syntax? The docs I've found are either from older versions or are incomplete. Is there a complete reference somewhere online?

Also, I'm struggling a bit here:

describe Favorite do
 describe "validations" do
   describe "#user" do
     let(:favorite) { Factory(:flowerhub, :user => user) }
     subject { favorite.errors[:user] }
     context "when user is nil" do
       let(:user) { nil }
       it { should == ["can't be blank"] }
     end
   end
 end
end

When I run this spec, it says:


  1) Favorite validations#user when user is nil 
     Failure/Error: let(:favorite) { Factory(:flowerhub, :user => user) }
     ActiveRecord::RecordInvalid:
       Validation failed: User can't be blank

Which make sense; it's not letting me create a factory with an invalid attribute (nil user). How would you go about testing the validation using this syntax?

Finally, why did you begin "#user" with #?

@nicholaides
Copy link

nicholaides commented Nov 28, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment