Rspec my validations for models
// There is also a bit at the bottom that checks that I have the right association
// setup.
require File.dirname(__FILE__) + '/../spec_helper' module CommentSpecHelper def valid_comment_attributes { :body => 'Some text', :commentable_type => 'School', :commentable_id => 1 } end end describe Comment do include CommentSpecHelper before(:each) do @comment = Comment.new end it "should be valid" do @comment.attributes = valid_comment_attributes @comment.should be_valid end it "should should not be valid without something to attach to" do c = valid_comment_attributes c.delete :commentable_id c.delete :commentable_type @comment.attributes = c @comment.should_not be_valid @comment.errors.on(:commentable_id).should eql("can't be blank") @comment.commentable_id = 1 @comment.should_not be_valid @comment.errors.on(:commentable_type).should eql("can't be blank") @comment.commentable_type = "School" @comment.should be_valid end it "should require body" do @comment.attributes = valid_comment_attributes.except(:body) @comment.should_not be_valid @comment.errors.on(:body).should eql("can't be blank") @comment.body = "Some text" @comment.should be_valid end it "should relate to commentable" do Comment.reflect_on_association(:commentable).should_not be_nil end it "should relate to user" do Comment.reflect_on_association(:user).should_not be_nil end end