Rails5 で rspec の expect を使ってみる
Ruby Ruby on Rails
Published: 2019-06-02

やったこと

Rails5 で rspec の expect を使ってみます。

確認環境

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]

$ rails --version
Rails 5.2.3

$ gem list | grep rspec-rails
rspec-rails (3.8.2)

調査

spec/models/task2_spec.rb

require 'rails_helper'

RSpec.describe Task, type: :model do
  it '計算1 should' do
    (1 + 2).should eq 3
  end

  it '計算2 expect' do
    expect(3 + 4).to eq 7
  end
end

出力結果

$ bundle exec rspec spec/models/task_spec.rb -f d

Task
  計算1 should
  計算2 expect

Deprecation Warnings:

Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }` instead. Called from /Users/xxxx/sample2/spec/models/task2_spec.rb:5:in `block (2 levels) in <top (required)>'.


If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.

1 deprecation warning total

Finished in 0.00259 seconds (files took 0.84059 seconds to load)
2 examples, 0 failures

should は非推奨になったので、警告が出ています。

should で発生する理由はこちらにあるようです(まだ理解していない)

参考