validates_inclusion_of,acts_as_tree和rspec的问题

时间:2010-03-10 11:20:04

标签: ruby-on-rails ruby validation rspec

我有问题让rspec正常运行以测试validates_inclusion_of我的迁移看起来像这样:

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

我的模型看起来像这样:

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

我的工厂:

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

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

我的模型规范如下:

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

我收到以下错误:

  

'类别应保存为有效   parent'FAILED预期#&lt;类别ID:   零,名称:“类别二”,parent_id:   5,created_at:nil,updated_at:nil&gt;   有效,但不是错误:

     

父母遗失

在控制台上,一切似乎都很好并按预期工作:

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

我正在运行rails 2.3.5,rspec 1.3.0和rspec-rails 1.3.2

任何人,不知道吗?

2 个答案:

答案 0 :(得分:5)

问题是您无法在被叫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测试将会通过。

答案 1 :(得分:0)

实际上,只需将Category.all.map(&:id)放入proc / lambda即可推迟可枚举计算。 写

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

将在验证时获取类别ID,而不是在类声明时。