LoginSignup
3
1

More than 1 year has passed since last update.

[Rails]RSpecでbinding.pryがNoMethodError: undefined method `pry' for *** になるときの対処法。

Last updated at Posted at 2022-04-30

RSpec初心者です。

RSpecのテストがどうしても通らないので、変数の中身を確認したい、という状況がありました。
調べてみるとexample中(it 'hoge' doの中)でbinding.pryが使えるらしいです。

しかし、NoMethodErrorが出てデバッグできませんでした。

Failures:

  1) DeviseSessions POST #sign_in パラメーターが有効な場合 ユーザー個別ページにリダイレクトされる(リクエストが成功する)こと
     Failure/Error: binding.pry
     
     NoMethodError:
       undefined method `pry' for #<Binding:0x00007f9d27b2d9c0>
       Did you mean?  try
...

原因

spec/spec_helper.rbrequire 'pry'が記載されていない。

参考:

対処法

spec/spec_helper.rbrequire 'pry'を追加します。

spec/spec_helper.rb
RSpec.configure do |config|
  require 'pry'   <=ここ!

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
  config.shared_context_metadata_behavior = :apply_to_host_groups
  config.filter_run_when_matching :focus
end
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1