Created
November 26, 2011 01:23
-
-
Save mattm/1394796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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 |
What Chris said. :)
Author
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 #?
The documentation for RSpec is pretty bad. Here's a good article.
http://blog.carbonfive.com/2010/10/21/rspec-best-practices/
For testing validation syntax, I'd do:
describe "user requirement" do
subject{ Factory.build(:flowerhub, :user => nil) }
it{ should_not be_valid }
it{ should have_errors_on(:user) } #or whatever the syntax is
end
Mike Nicholaides
http://ablegray.com
(267) 622-4729
…On Sat, Nov 26, 2011 at 10:33 PM, mattm < ***@***.*** > wrote:
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?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1394796
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've become a fan of the context and let syntax in rspec.
This is a decent post on rspec best practices:
http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/