测试我的 Ruby gem:Shoulda::Matchers:Module 的未定义方法“configure”(NoMethodError)

testing my Ruby gem: undefined method `configure' for Shoulda::Matchers:Module (NoMethodError)

我正在研究我的第一个 Ruby gem 并捆绑了黄瓜、rspec 和 shoulda-matches 用于测试。当我 运行 rspec 时,出现以下错误:

/app/my_gem/spec/spec_helper.rb:6:in `<top (required)>': undefined method `configure' for Shoulda::Matchers:Module (NoMethodError)

这是我的 gem规格:

# my_gem.gemspec
...
Gem::Specification.new do |spec|
  ...
  ...
  spec.add_development_dependency "activemodel"
  spec.add_development_dependency "bundler", "~> 1.8"
  spec.add_development_dependency "cucumber"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "rspec" 
  spec.add_development_dependency "shoulda-matchers"
end

我的spec_helper.rb:

require 'my_gem'
require 'pry'
require 'shoulda/matchers'

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec

    # with.library :active_record
    with.library :active_model
    # with.library :action_controller
    # Or, choose all of the above:
    # with.library :rails
  end
end

它正在寻找 Shoulda::Matchers 但出于某种原因没有找到 .configure 方法。我要求 shoulda 是不是错了?不确定这是否相关,但 rspec 也给了我这个警告:

WARN: Unresolved specs during Gem::Specification.reset:
  json (>= 1.7.7, ~> 1.7)
  minitest (~> 5.1)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.

感谢指点!

看起来您正在尝试使用支持 3.0.0.alpha 的 3.0.0.alpha 版本的 shoulda-matchers 的文档,但您使用的是旧版本。要么查看您正在使用的版本的正确文档(我猜 2.8.x),要么更新您的 Gemfile 以使用 3.0.0.alpha:

gem 'shoulda-matchers', github: 'thoughtbot/shoulda-matchers'

然后 运行 bundle installShoulda::Matchers.configure 应该开始工作了。

Git 协议已贬值,现在使用:

   gem 'shoulda-matchers', git: 'https://github.com/thoughtbot/shoulda-matchers.git'

(根据 https://github.com/thoughtbot/shoulda-matchers/issues/719#issuecomment-103556439

@infused提供的解决方案是正确的

GitHub 中提供的文档和配置适用于版本 3.x,不适用于 2.x。

为了使其正常工作,将 should-matchers 的版本更改为“~> 3.0.0.alpha”,然后 运行 'bundle update should-matchers'