在创业板文件中,我写了以下内容:
group :test do
gem 'rspec-rails'
gem 'shoulda-matchers', require: false
gem 'factory_girl'
end我的rails_helper
require 'rspec/rails'
require 'shoulda/matchers'我的spec_helper
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最后我的测试是:
require 'spec_helper'
describe Person do
it {should validate_presence_of(:identifier)}
end而我却不知道
Failure/Error: it {should validate_presence_of(:identifier)}
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Person:0x007fe58d13ca20>我就是不明白。在其他项目中,同样的工作方式。这里没有。我花了两天时间来解决这个问题。在rails中是否有更好的Shoulda Matcher的替代方案?
发布于 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的其余部分)应该被加载。
发布于 2015-10-28 23:33:07
将Gemfile更改为使用两个不同的组进行测试,如下所示:
group :test, :development do
gem 'rspec-rails'
gem 'factory_girl'
end
group :test do
gem 'shoulda-matchers', require: false
end这过去是在shoulda加载Test::Unit而不是RSpec时造成的,但我认为这是固定的。
https://stackoverflow.com/questions/33403298
复制相似问题