Rails模型包括验证但无法找到验证方法

时间:2013-06-17 18:06:19

标签: ruby-on-rails rspec rspec-rails spree

我在我的rspec测试中遇到了一个非常好奇的失败,并将其追溯到正在运行验证调用的模型,但是其实际验证方法似乎找不到。这是它的样子:

Failure/Error: FactoryGirl.create(:spree_user_subscription)
NoMethodError:
   undefined method `cannot_have_more_than_one_periodical' for #<Spree::Product:0x007fb15d1baf88>

这是Spree :: Product装饰器剪切以显示相关的行:

product_decorator.rb(摘录):

. . .

Spree::Product.class_eval do

  . . .

  attr_accessible :subscription

  . . .

  after_initialize :mixin_subscription_logic, :if => :subscription?

  scope :subscriptions_first, reorder("subscription DESC","for_sale_on DESC")

  scope :subscriptions, where(:subscription => true).reorder(:name)

  . . .

  def subscription=(value)
    write_attribute(:subscription, value)
    mixin_subscription_logic if subscription?
  end

  def metaclass
    class << self
      self
    end
  end

  private

  def mixin_subscription_logic
    metaclass.send(:include, Spree::Subscription)
  end
end

这就是订阅。

subscription.rb

module Spree::Subscription
  extend ActiveSupport::Concern

  included do
    validate :cannot_have_more_than_one_periodical
  end

  def periodical
    periodical_taxons.first
  end

  private
  def periodical_taxons
    taxons.select { |taxon| taxon.type == 'Periodical' }
  end

  def cannot_have_more_than_one_periodical
    if periodical_taxons.length > 1
      errors[:base] << 'A subscription cannot have more than one periodical.'
    end
  end
end

因此,在我看来,为了获得undefined method 'cannot_have_more_than_one_periodical'错误,必须调用validate :cannot_have_more_than_one_periodical,但必须以某种方式找不到def cannot_have_more_than_one_periodical

有没有人对如何做到这一点有任何想法?

顺便说一句,这是完整的堆栈跟踪:

2) UserSubscriptionsController PUT #update same billing address submitted does not create a new address
 Failure/Error: FactoryGirl.create(:spree_user_subscription)
 NoMethodError:
   undefined method `cannot_have_more_than_one_periodical' for #<Spree::Product:0x007fb15d1baf88>
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/bundler/gems/spree_related_products-060fb20c443e/app/models/spree/product_decorator.rb:48:in `method_missing'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:627:in `_run__2367995844349070862__validate__810061369413105445__callbacks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activemodel-3.2.13/lib/active_model/validations.rb:228:in `run_validations!'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activemodel-3.2.13/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:414:in `_run__2367995844349070862__validation__810061369413105445__callbacks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activemodel-3.2.13/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activemodel-3.2.13/lib/active_model/validations.rb:195:in `valid?'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/validations.rb:69:in `valid?'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/validations.rb:77:in `perform_validations'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/validations.rb:56:in `save!'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/transactions.rb:264:in `block in save!'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/newrelic_rpm-3.6.3.111/lib/new_relic/agent/method_tracer.rb:523:in `block in transaction_with_trace_ActiveRecord_self_name_transaction'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/newrelic_rpm-3.6.3.111/lib/new_relic/agent/method_tracer.rb:273:in `trace_execution_scoped'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/newrelic_rpm-3.6.3.111/lib/new_relic/agent/method_tracer.rb:518:in `transaction_with_trace_ActiveRecord_self_name_transaction'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/activerecord-3.2.13/lib/active_record/transactions.rb:264:in `save!'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/definition.rb:9:in `block in initialize'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/strategy/create.rb:11:in `[]'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/strategy/create.rb:11:in `block in result'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/strategy/create.rb:9:in `tap'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/strategy/create.rb:9:in `result'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/factory.rb:48:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/factory_runner.rb:25:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/strategy/create.rb:5:in `association'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/evaluator.rb:45:in `association'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/attribute/association.rb:15:in `block in to_proc'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/evaluator_class_definer.rb:29:in `instance_exec'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/evaluator_class_definer.rb:29:in `block in define_attribute'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/attribute_assigner.rb:36:in `get'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/attribute_assigner.rb:14:in `block (2 levels) in object'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/attribute_assigner.rb:13:in `each'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/attribute_assigner.rb:13:in `block in object'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/attribute_assigner.rb:12:in `tap'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/attribute_assigner.rb:12:in `object'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/strategy/create.rb:9:in `result'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/factory.rb:48:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/factory_runner.rb:25:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/factory_girl-2.6.4/lib/factory_girl/syntax/methods.rb:63:in `create'
 # ./spec/controllers/user_subscriptions_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/let.rb:33:in `instance_eval'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/let.rb:33:in `block (2 levels) in let'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/let.rb:33:in `fetch'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/let.rb:33:in `block in let'
 # ./spec/controllers/user_subscriptions_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:237:in `instance_eval'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:237:in `instance_eval'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/hooks.rb:23:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/hooks.rb:72:in `block in run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/hooks.rb:72:in `each'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/hooks.rb:72:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/hooks.rb:424:in `run_hook'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:332:in `run_before_each_hooks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:300:in `run_before_each'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:113:in `block in run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:111:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:388:in `block in run_examples'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `map'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `run_examples'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:369:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `block in run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `map'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `block in run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `map'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `map'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block in run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/reporter.rb:34:in `report'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:25:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
 # ~/.rvm/gems/ruby-1.9.3-p392@myappname/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'

0 个答案:

没有答案