ruby-on-rails - 在单元测试中使用 HABTM Fixture 的 Rails 单表继承返回 NoMethodError : undefined method `singularize'

标签 ruby-on-rails unit-testing fixtures has-and-belongs-to-many single-table-inheritance

想象一个模型结构如下:

模型/cross_sell_promotion.rb

     class CrossSellPromotion < Promotion
       has_and_belongs_to_many :afflicted_products, :join_table => :promotion_afflicted_products, :foreign_key => 'promotion_id', :association_foreign_key => 'product_id', :class_name => 'Product'
       has_and_belongs_to_many :afflicted_categories, :join_table => :promotion_afflicted_categories, :foreign_key => 'promotion_id', :association_foreign_key => 'category_id', :class_name => 'Category'
       . . .
     end

模型/促销.rb

class Promotion < ActiveRecord::Base
  has_and_belongs_to_many :products, :join_table => :promotions_products
  has_and_belongs_to_many :categories, :join_table => :promotions_categories
  - - -
end

和表格如下:

table promotions
type :string
...some other fields unrelated to this problem

table promotions_products
promotion_id :integer
product_id :integer

table promotion_afflicted_products
promotion_id :integer
product_id :integer

table promotion_afflicted_categories
promotion_id :integer
category_id :integer

然后我有一个固定装置如下:

#promotions.yml
cross_sell_1:
products: plumeboom_1, plumeboom_2
  value: 100
  value_type: percentage
  type: CrossSellPromotion

#products.yml
plumeboom_1:
  model: plumeboom1
  url: plumeboom-1

plumeboom_2:
  model: plumeboom2
  url: plumeboom-2

plumeboom_3:
  model: plumeboom3
  url: plumeboom-3

当我运行我的单元测试时,它返回此错误消息:

1) Error:
test_the_truth(CartTest):
NoMethodError: undefined method `singularize' for :promotions_products:Symbol

如果您碰巧有类似的经历或知道如何解决这个问题,或者至少只是您认为可能是错误的地方,请告诉我,我准备好尝试一切!

非常感谢,请不要犹豫回复...这里真的很绝望!

最佳答案

答案是......你不能!

遗憾的是,有时 Rails 无法解决问题。在这些情况下,只需为关联表创建另一个 Fixture。

例如在本例中为 promotions_products.yml 并在其中输入:

pp1:
    promotion_id: <%= Fixtures.identify(:cross_sell_1) %>
    product_id: <%= Fixtures.identify(:plumeboom_1) %>

我设法在 fixtures 中使用 HABTM 连接,但是当它们也有 STI(单表继承)时,rails 似乎有问题。

关于ruby-on-rails - 在单元测试中使用 HABTM Fixture 的 Rails 单表继承返回 NoMethodError : undefined method `singularize' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2369895/

相关文章:

mysql - 带有 .new 的 Rails 控制台出错

python - 在 python 中模拟类上的多个方法的干净方法是什么?

java - 如何在 Spring 单元测试中注入(inject) ServletContext?

java - 对于初学者 java 网络程序来说,什么是一个好的测试方案

c# - 如何在所有测试类中共享一个 fixture ?

Swift 包管理器,如何处理固定装置?

ruby-on-rails - Heroku 配置 keystore 错误

ruby-on-rails - 如何使用 Windows 开始使用 Ruby on Rails

ruby-on-rails - POST 请求在 Heroku 环境中被视为 GET

python - 如何设置 web2py fixtures