validates_inclusion_of、acts_as_tree 和 rspec 的问题 [英] Problems with validates_inclusion_of, acts_as_tree and rspec

查看:68
本文介绍了validates_inclusion_of、acts_as_tree 和 rspec 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使 rspec 正常运行以测试我的迁移的 validates_inclusion_of 时遇到问题:

I have problems to get rspec running properly to test validates_inclusion_of my migration looks like this:

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

我收到以下错误:

'类别应保存为有效父母'失败预期#<类别ID:无,名称:第二类",parent_id:5、created_at:nil、updated_at:nil>有效,但不是错误:

'Category should save with valid parent' FAILED Expected #<Category id: nil, name: "Category Two", parent_id: 5, created_at: nil, updated_at: nil> to be valid, but it was not Errors:

父母不见了

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

On console everything seems to be fine and work 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

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

I'm running rails 2.3.5, rspec 1.3.0 and rspec-rails 1.3.2

有人知道吗?

推荐答案

问题是你不能在被调用的对象内部调用Category.all.map(&:id)validates_inclusion_of.

The problem is that you can't put a call to Category.all.map(&:id) inside the called to validates_inclusion_of.

当您尝试运行时,这种情况的第一个迹象会很明显

The first indication that this is the case will be apparent when you try to run

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

其中 是创建类别模型的迁移的版本号.

where <n> is the version number of the migration that creates the Category model.

你会得到类似的东西:

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 模型不存在它失败了.

This is because rake tries to load app/models/category.rb before running the migration. Because the Category model does not exist it fails.

查看问题的另一种方法是执行tail -f log/development.log,然后尝试使用script/console 打开控制台.您将看到以下形式的 SQL 查询:

Another way to see the problem is to do tail -f log/development.log and then try to open a console with script/console. You will see an SQL query of the form:

SELECT * FROM "categories"

在输出中.这对应于对 Category.all.map(:&id) 的调用.但是,一旦您开始输入以下命令:

in the output. This corresponds to the call to Category.all.map(:&id). However, once you start typing commands like:

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

您将看到查询 SELECT * from "categories" 不再出现在日志中.这个故事的寓意是只有常量可以出现在对validations_inclusion_of的调用中,因为那里的代码只会被评估一次..

you will see that the query SELECT * from "categories" does not reappear in the log. The moral of the story is only constants can appear in calls to validations_inclusion_of because the code in there will only be evaluated once..

您的控制台代码工作的唯一原因是,在之前的控制台会话中,您使用 id=1

The only reason your console code worked was because, in a previous console session, you had created a Category object with id=1

您可以编写一个自定义验证来执行您想要的操作:

You can write a custom validation that does what you want with:

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 测试将通过.

Your rspec tests will pass once you have added this code.

这篇关于validates_inclusion_of、acts_as_tree 和 rspec 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆