Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Rspec my validations for models (See related posts)

// This is the plain-vanilla way that I've been using spec for basic model validations.
// 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

Comments on this post

chrisdpratt posts on Sep 07, 2007 at 15:15
It took me a second to figure out what you were doing with these lines:
   1  
   2      c = valid_comment_attributes
   3      c.delete :commentable_id
   4      c.delete :commentable_type
   5      @comment.attributes = c

Because I always use hash extensions such as #except. So after I snapped that you're removing two indexes from the hash, I moved on assuming "Okay, just not using hash extensions". But, then I see this line later in your code:

   1  @comment.attributes = valid_comment_attributes.except(:body)


You do realize that you can remove more than one index with except at a time right? Instead of the four lines of code above, you could have just wrote:

   1  @comment.attributes = valid_comment_attributes.except(:commentable_id, :commentable_type)


Assuming of course that you're going to be using the hash extensions anyways...

You need to create an account or log in to post comments to this site.


Click here to browse all 5556 code snippets

Related Posts