この記事は古くなったか、僕の勘違いでした。
詳しくは shoulda-matchers/README.md at master · thoughtbot/shoulda-matchers を読んでいただくとして……。
gem 'shoulda-matchers'
ではなく
gem 'shoulda'
とすれば良いです。
spring を使ってて shoulda-matchers がうまく動かなかったのでメモ
アプリを作ろうと考える
% rails new myapp -T
% cd myapp
Gemfile を書く書く
Gemfile
source 'https://rubygems.org'
ruby '2.2.1'
gem 'rails', '4.2.1'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'rspec-rails'
end
group :development do
gem 'spring-commands-rspec' # rspec を spring でしてくれる
end
group :test do
gem 'shoulda-matchers' # テストを書くのが楽になる
end
環境を整えて User モデルを作る
% bin/bundle
% bin/spring stop
% bin/rails generate rspec:install
% bin/spring binstub rspec
% bin/rails generate model user name:string
% bin/rake db:migrate
バリデーションを書く書く
app/models/user.rb
class User < ActiveRecord::Base
validates :name, presence: true
end
バリデーションのテストを書く書く
spec/models/user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
it { should validate_presence_of :name }
end
さあテストを実行だ
% bin/rspec spec/models/user_spec.rb
F
Failures:
1) User
Failure/Error: it { should validate_presence_of :name }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User:0x007fae89c104f0>
# ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
# /Users/maangie/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
# -e:1:in `<main>'
Finished in 0.00139 seconds (files took 0.61862 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:4 # User
……。なんでやねん
spring の所為かなあ…
% rspec spec/models/user_spec.rb
.
Finished in 0.01885 seconds (files took 4.39 seconds to load)
動いたorz
対処法
Gemfile
と rails_helper.rb
を書き換えて…
Gemfile
gem 'shoulda-matchers', require: false
spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'shoulda-matchers' # ココ
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
bundle
する
% bin/bundle
実行結果
% bin/rspec spec/models/user_spec.rb
.
Finished in 0.01917 seconds (files took 0.83555 seconds to load)
1 example, 0 failures
以下を参考にしました
http://ruby-rails.hatenadiary.com/entry/20141026/1414289421#spring-additional-commands
https://github.com/thoughtbot/shoulda-matchers/issues/486