Rspec my validations for models
// There is also a bit at the bottom that checks that I have the right association
// setup.
1 2 require File.dirname(__FILE__) + '/../spec_helper' 3 4 module CommentSpecHelper 5 def valid_comment_attributes 6 { :body => 'Some text', 7 :commentable_type => 'School', 8 :commentable_id => 1 } 9 end 10 end 11 12 describe Comment do 13 14 include CommentSpecHelper 15 16 before(:each) do 17 @comment = Comment.new 18 end 19 20 it "should be valid" do 21 @comment.attributes = valid_comment_attributes 22 @comment.should be_valid 23 end 24 25 it "should should not be valid without something to attach to" do 26 c = valid_comment_attributes 27 c.delete :commentable_id 28 c.delete :commentable_type 29 @comment.attributes = c 30 @comment.should_not be_valid 31 @comment.errors.on(:commentable_id).should eql("can't be blank") 32 @comment.commentable_id = 1 33 @comment.should_not be_valid 34 @comment.errors.on(:commentable_type).should eql("can't be blank") 35 @comment.commentable_type = "School" 36 @comment.should be_valid 37 end 38 39 it "should require body" do 40 @comment.attributes = valid_comment_attributes.except(:body) 41 @comment.should_not be_valid 42 @comment.errors.on(:body).should eql("can't be blank") 43 @comment.body = "Some text" 44 @comment.should be_valid 45 end 46 47 it "should relate to commentable" do 48 Comment.reflect_on_association(:commentable).should_not be_nil 49 end 50 51 it "should relate to user" do 52 Comment.reflect_on_association(:user).should_not be_nil 53 end 54 end