首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在Shoulda Matcher做了什么错事?

我在Shoulda Matcher做了什么错事?
EN

Stack Overflow用户
提问于 2015-10-28 23:20:53
回答 2查看 714关注 0票数 1

在创业板文件中,我写了以下内容:

代码语言:javascript
复制
group :test do
  gem 'rspec-rails'
  gem 'shoulda-matchers', require: false
  gem 'factory_girl'
end

我的rails_helper

代码语言:javascript
复制
require 'rspec/rails'
require 'shoulda/matchers'

我的spec_helper

代码语言:javascript
复制
require 'rails_helper'


RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.include FactoryGirl::Syntax::Methods




  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end


  Shoulda::Matchers.configure do |config|

    config.integrate do |with|
      # Choose a test framework:
      with.test_framework :rspec

      # Choose one or more libraries:
      with.library :active_record
      with.library :active_model
      with.library :action_controller
    end
  end

最后我的测试是:

代码语言:javascript
复制
require 'spec_helper'
describe Person do

  it {should validate_presence_of(:identifier)}
end

而我却不知道

代码语言:javascript
复制
     Failure/Error: it {should validate_presence_of(:identifier)}
 NoMethodError:
   undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Person:0x007fe58d13ca20>

我就是不明白。在其他项目中,同样的工作方式。这里没有。我花了两天时间来解决这个问题。在rails中是否有更好的Shoulda Matcher的替代方案?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-29 18:02:11

抱歉你有麻烦了。你需要先加载钢轨,然后再需要配戴。gem只有在可以看到ActiveRecord可用时才会使模型匹配器可用;如果没有,则不会得到它们。

看来您已经交换了rails_helper和spec_helper。通常,在安装了Rails的新项目中,rails_helper将加载Rails,然后需要spec_helper。我会删除这两个文件(或者将它们移到一边,以便您仍然拥有它们),然后重新运行rails g rspec:install来重新生成它们。您仍然希望像您所做的那样将肩匹配器配置块添加到rails_helper中,但是到那时,ActiveRecord (以及Rails的其余部分)应该被加载。

票数 1
EN

Stack Overflow用户

发布于 2015-10-28 23:33:07

将Gemfile更改为使用两个不同的组进行测试,如下所示:

代码语言:javascript
复制
group :test, :development do
  gem 'rspec-rails'
  gem 'factory_girl'
end

group :test do
  gem 'shoulda-matchers', require: false
end

这过去是在shoulda加载Test::Unit而不是RSpec时造成的,但我认为这是固定的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33403298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档