[英]What am I doing wrong in Shoulda Matchers?
在gem文件中,我這樣寫:
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>
我根本不明白。 在其他項目中,以相同的方式工作。 在這里沒有。 我花了兩天時間來解決此問題。 在軌道中是否有更好的Shoulda Matchers替代品?
將您的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加載而引起的,但是我認為這是固定的。
抱歉,您遇到了麻煩。 您需要先安裝Rails匹配器再安裝Rails。 gem僅在看到ActiveRecord可用時才會使模型匹配器可用; 如果沒有,那么您將不會得到它們。
看來您已經交換了rails_helper和spec_helper。 通常,在安裝了rspec-rails的新項目中,rails_helper將加載Rails,然后需要spec_helper。 我將刪除這兩個文件(或將它們移到一邊,以便您仍然擁有它們),然后重新運行rails g rspec:install
以重新生成它們。 您仍然需要像完成操作一樣將shoulda-matchers配置塊添加到rails_helper中,但是到那時,應該加載ActiveRecord(和其余的Rails)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.