Problems with validates_inclusion_of, actions_as_tree and rspec

I am having trouble getting rspec to work correctly, to check the validates_inclusion_of of my migration is as follows:

class CreateCategories < ActiveRecord::Migration
  def self.up
    create_table :categories do |t|
      t.string :name
      t.integer :parent_id
      t.timestamps
    end
  end

  def self.down
    drop_table :categories
  end
end

my model looks like this:

class Category < ActiveRecord::Base
  acts_as_tree

  validates_presence_of :name
  validates_uniqueness_of :name
  validates_inclusion_of :parent_id, :in => Category.all.map(&:id), :unless => Proc.new { |c| c.parent_id.blank? }
end

my plants:

Factory.define :category do |c|
  c.name "Category One"
end

Factory.define :category_2, :class => Category do |c|
  c.name "Category Two"
end

my model specification is as follows:

require 'spec_helper'

describe Category do
  before(:each) do
    @valid_attributes = {
      :name => "Category"
    }
  end

  it "should create a new instance given valid attributes" do
    Category.create!(@valid_attributes)
  end

  it "should have a name and it shouldn't be empty" do
    c = Category.new :name => nil
    c.should be_invalid
    c.name = ""
    c.should be_invalid
  end

  it "should not create a duplicate names" do
    Category.create!(@valid_attributes)
    Category.new(@valid_attributes).should be_invalid
  end

  it "should not save with invalid parent" do
    parent = Factory(:category)
    child = Category.new @valid_attributes
    child.parent_id = parent.id + 100
    child.should be_invalid
  end

  it "should save with valid parent" do
    child = Factory.build(:category_2)
    child.parent = Factory(:category)
    # FIXME: make it pass, it works on cosole, but I don't know why the test is failing
    child.should be_valid
  end
end

I get the following error:

'Category must save with valid parent "FAILED Expected # <Category id: nil, name:" Category second ", parent_id: 5, created_at: nil, updated_at: nil> to be valid, but this was not an error:

Missing Parent

Everything seems fine on the console and works as expected:

c1 = Category.new :name => "Parent Category"
c1.valid? #=> true
c1.save #=> true
c1.id #=> 1
c2 = Category.new :name => "Child Category"
c2.valid? #=> true
c2.parent_id = 100
c2.valid? #=> false
c2.parent_id = 1
c2.valid? #=> true

I run rails 2.3.5, rspec 1.3.0 and rspec-rails 1.3.2

Anyone any idea?

+3
2

, Category.all.map(&:id) validates_inclusion_of.

, ,

rake db:migrate:down VERSION=<n>
rake db:migrate:up VERSOIN=<n>

<n> - , .

- :

in /Users/sseefried/tmp/so)
==  CreateCategories: reverting ===============================================
-- drop_table(:categories)
    -> 0.0032s
==  CreateCategories: reverted (0.0034s) ======================================

(in /Users/sseefried/tmp/so)
rake aborted!
SQLite3::SQLException: no such table: categories: SELECT * FROM "categories" 

(See full trace by running task with --trace)

, rake app/models/category.rb . Category , .

- tail -f log/development.log, script/console. SQL- :

SELECT * FROM "categories"

. Category.all.map(:&id). , , :

c1 = Category.new, :name => "Category 1"

, SELECT * from "categories" . validations_inclusion_of , ..

, , , Category id=1

, , :

validate :parent_exists

protected

def parent_exists
  ids = Category.all.map(&:id)
  if !parent_id.blank? && !ids.member?(parent_id)
    errors.add(:parent_id, "does not point to a valid parent record")
  end
end

rspec .

+5

, , Category.all.map(&:id) proc/lambda.

  validates_inclusion_of :parent_id,
                         in: proc{ Category.all.map(&:id) },
                         unless: proc{ |c| c.parent_id.blank? }

, .

0

Source: https://habr.com/ru/post/1736287/


All Articles