Rspec and Rails 3 - Problem Validating Nested Attribute Collection Size

Posted by MunkiPhD on Stack Overflow See other posts from Stack Overflow or by MunkiPhD
Published on 2010-12-27T03:52:31Z Indexed on 2010/12/27 3:53 UTC
Read the original article Hit count: 164

When I create my Rspec tests, I keep getting a validation of false as opposed to true for the following tests. I've tried everything and the following is the measly code that I have now - so if it's waaaaay wrong, that's why.

class Master < ActiveRecord::Base
      attr_accessible :name, :specific_size

      # Associations ----------------------
      has_many :line_items

      accepts_nested_attributes_for :line_items,
        :allow_destroy => true,
        :reject_if => lambda { |a| a[:item_id].blank? }

      # Validations -----------------------
      validates :name, 
                  :presence => true, 
                  :length => {:minimum => 3, :maximum => 30}
      validates :specific_size, 
                  :presence => true, 
                  :length => {:minimum => 4, :maximum => 30}

      validate :verify_items_count

      def verify_items_count
          if self.line_items.size < 2
            errors.add(:base, "Not enough items to create a master")
          end
      end
    end

And here it the items model:

class LineItem < ActiveRecord::Base
  attr_accessible :specific_size, :other_item_type_id

  # Validations --------------------
  validates :other_item_type_id,
                :presence => true
  validates :master_id,
                :presence => true
  validates :specific_size,
                :presence => true

  # Associations ---------------------
  belongs_to :other_item_type
  belongs_to :master
end

The RSpec Tests:

    before(:each) do
          @master_lines = []
          @master_lines << LineItem.new(:other_item_type_id => 1, :master_id => 2, :specific_size => 1) 
          @master_lines << LineItem.new(:other_item_type_id => 2, :master_id => 2, :specific_size => 1) 
          @attr = {:name => "Some Master", :specific_size => "1 giga"}
        end

        it "should create a new instance given a valid name and specific size" do
          @master = Master.create(@attr)

          line_item_one = @master.line_items.build(:other_item_type_id => 1, :specific_size => 1)
          line_item_two = @master.line_items.build(:other_item_type_id => 2, :specific_size => 2)

          @master.line_items.size === 2

          @master.should be_valid
        end

 it "should have at least two items to be valid" do
      master = Master.new(:name => "test name", :specific_size => "1 mega")

      master_item_one = LineItem.new(:other_item_type_id => 1, :specific_size => 2)
      master_item_two = LineItem.new(:other_item_type_id => 2, :specific_size => 1)
      master.line_items << master_item_one
      master.should_not be_valid

      master.line_items << master_item_two
      master.line_items.size.should === 2
      master.should be_valid
    end

I'm very new to Rspec and Rails - and I've been failing at this for the past couple of hours. Thanks for any help in advance.

© Stack Overflow or respective owner

Related posts about validation

Related posts about ruby-on-rails3