0

我有以下内容:

# lib/devblog_extensions.rb
module DevblogExtensions
  class Time
    def self.random_date
      # implementation ...
    end
  end
end

# spec/factories/posts.rb
FactoryGirl.define do
  factory :post do
    release_date Time.random_date # NoMethodError occurs
  end
end

然后在config/application.rb我有:

config.autoload_paths += %W(#{config.root}/lib)

但是当我跑步时,rspec我仍然得到一个NoMethodError,所以我错过了什么?谢谢!

4

1 回答 1

0

您可能缺少包括

# spec/factories/posts.rb
include DevblogExtensions

FactoryGirl.define do
  factory :post do
    release_date Time.random_date # NoMethodError occurs
  end
end

更新:快速修复

# lib/devblog_extensions.rb
module DevblogExtensions
end

class Time
  def self.random_date
    Time.now
  end
end
于 2012-08-03T17:14:54.190 回答