2011-11-22 18 views
1

在我的新式可安装引擎的单元测试期间,我遇到了一个问题。导轨安装式发动机内测试夹具中的关联

我有两种基本模式:

module Ads 
    class Category < ActiveRecord::Base 
    set_table_name :categories 
    has_many :ads 
    end 
end 

module Ads 
    class Ad < ActiveRecord::Base 
    set_table_name :ads 
    belongs_to :category 
    end 
end 

它可以在铁轨控制台完美的罚款。

ruby-1.9.2-p290 :007 > c = Ads::Category.create(title: 'foo') 
(0.4ms) BEGIN 
SQL (1.0ms) INSERT INTO "categories" ("created_at", "title", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Tue, 22 Nov 2011 15:34:41 UTC +00:00], ["title", "foo"], ["updated_at", Tue, 22 Nov 2011 15:34:41 UTC +00:00]] 
(18.0ms) COMMIT 
=> #<Ads::Category id: 2, title: "foo", created_at: "2011-11-22 15:34:41", updated_at: "2011-11-22 15:34:41"> 
ruby-1.9.2-p290 :008 > c.ads.create(title: 'bar') 
(0.6ms) BEGIN 
SQL (1.1ms) INSERT INTO "ads" ("category_id", "created_at", "title", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["category_id", 2], ["created_at", Tue, 22 Nov 2011 15:34:43 UTC +00:00], ["title", "bar"], ["updated_at", Tue, 22 Nov 2011 15:34:43 UTC +00:00]] 
(16.8ms) COMMIT 
=> #<Ads::Ad id: 2, title: "bar", category_id: 2, created_at: "2011-11-22 15:34:43", updated_at: "2011-11-22 15:34:43"> 

我转移到一个单元测试:填充测试分贝期间

rake app:test:units 

它以错误结束:

# test/unit/ads/category_test.rb 
require 'test_helper' 

module Ads 
    class CategoryTest < ActiveSupport::TestCase 
    # by default it searches in dummy app, is there any cleaner way to change this? 
    self.fixture_path = Rails.root.parent + "./fixtures/ads" 
    fixtures :categories, :ads 

    test "should find cars" do 
     assert_equal 1, Category.where(title: 'Cars').count 
    end 
    end 
end 

# test/fixtures/ads/categories.yml 
cars: 
    title: Cars 

# test/fixtures/ads/ads.yml 
foo: 
    title: Foo 
    category: cars 

当运行单元测试

ActiveRecord::StatementInvalid: PGError: ERROR: column "category" of relation "ads" does not exist 
LINE 1: INSERT INTO "ads" ("title", "category") VALUES ('Foo', 'cars... 

看起来像广告< - > c ategories协会“被忽略。 在独立的rails应用程序中,相同的方法可以完美地工作。 我的问题是:我做错了什么? 侧面的问题是,如果有一个更清洁的解决方案来改变灯具路径?

Rails:3.1.3

回答

0

我有完全相同的问题,但有一个Rails 3.0引擎。我无法正确地修复它,但作为一项解决方案,我做到了这一点。

相反的:

category: cars 

务必:

category_id: <%= Fixtures.identify(:cars) %> 

这工作,因为ID实际上是标签的哈希值。 Fixtures.identify是什么哈希。