【问题标题】:Draper and RSpec and Factory Girl Controller Specs on Rails 4 AppRails 4 App 上的 Draper 和 RSpec 以及 Factory Girl 控制器规范
【发布时间】:2014-02-01 05:47:47
【问题描述】:

我是第一次使用 Draper,它工作得很好……除了它打破了我的大部分规格。一个例子:

我正在设置一个 account_settings_controller_spec.rb

describe '#create' do
  before do
    @user = create :user
   end
end

这之前工作得很好(我有 config.include FactoryGirl::Syntax::Methods 设置为这样调用工厂)

但是现在当我调用create :user 时,我得到了undefined method 'full_name' for class 'User',这是我从我的用户模型转移到我的用户装饰器的一种方法。

这很奇怪,因为工厂和规范都使用full_name 方法。

我尝试了多种方法:

  • ApplicationController.new.set_current_view_context 在 before 块中(这会导致错误)
  • require 'draper/test/rspec_integration' 到我的 spec_helper.rb(什么都不做)
  • before/after(:create) { |instance| instance.decorate } 添加到我的工厂(也无济于事)

我不确定我在这里遗漏了什么,甚至不知道为什么会抛出错误。当 RSpec 启动时,也许我的装饰器没有被包括在内?

【问题讨论】:

    标签: ruby-on-rails rspec ruby-on-rails-4 factory-bot draper


    【解决方案1】:

    啊哈!

    我知道我很愚蠢。

    因为我使用了friendly_id

    class User < ActiveRecord::Base
      extend FriendlyId
      friendly_id :slug_candidates, use: :slugged
    
      ...
    
      def slug_candidates
        [:full_name, :id]
      end
    end
    

    我刚刚将 full_name 方法移回模型,它就可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 2014-01-12
      • 2015-01-14
      • 1970-01-01
      • 2019-02-16
      相关资源
      最近更新 更多