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