CubicLouve

Spring_MTの技術ブログ

rspecを2.14.1から3.0.0に上げた時のメモ

追記 : transpecについて少し追記しました

railsアプリで使っていたrspecを2.14.1から3.0.0にあげたら色々落ちたので、それを直したメモ。

まず、

bundle exec rails generate rspec:install 

して、spec/spec_helper.rb.rspecを上書きした。

で、新たにspec/rails_helper.rbなるものが出現した。

(spec/spec_helper.rbは予めバックアップとっておいた)

https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/changelog をみると

Separate RSpec configuration in generated spec_helper from Rails setup and associated configuration options. 
Moving Rails specific settings and options to rails_helper. (Aaron Kromer)

てあるので、rails_helperにRailsに関わる設定を移した。(database_rewinderの設定とか)

でひとまず、

bundle exec rspec

したところ、warningsがうざいので、.rspec

--warnings

を消した。

で、幾つかエラーが出た。

comparison of Symbol with Module failed

describe :hoge do
end

って書いている部分だった。 SymbolをStringに変えたら通った。

undefined method `any_instance' for Hoge:Class

mockのsyntaxが変わっています。 spec_helperに下記のように書いていると新しいsyntaxが使われ落ちます。

  config.mock_with :rspec do |mocks|
    # Enable only the newer, non-monkey-patching expect syntax.
    # For more details, see:
    #   - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
    mocks.syntax = :expect                                                                                                        

    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended.
    mocks.verify_partial_doubles = true
  end

workaroundするなら、mocks.syntax = :expectの部分をコメントアウトしちゃえばOKです。

直すなら

-      Hoge.any_instance.stub(:foo).and_return(bar)
+      allow_any_instance_of(Hoge).to receive(:foo).and_return(bar)

みたいな感じにします。 詳細はこちらを見てください。

https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/method-stubs/stub-on-any-instance-of-a-class

expected true to respond to true?expected false to respond to false?

https://www.relishapp.com/rspec/rspec-expectations/v/3-0/docs/changelog

Rename be_true and be_false to be_truthy and be_falsey. (Sam Phippen)

にあったので、一括置換

undefined method `stub_model' for

https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/changelog

Extracts the mock_model and stub_model methods to the rspec-activemodel-mocks gem. (Thomas Holmes)

とあったので、rspec-activemodel-mocksを入れて解決

まとめ

こまめにアップデートしておけばよかった。。。。

一括置換するならこちらを参考するとよいかもです。

codemodを使って一括置換 - CubicLouve

id:sue445 さんからtranspecをオススメしていただきました!

transpecのほうがいいですね!

https://github.com/yujinakayama/transpec