Running ruby debug in rspec?

后端 未结 6 1367
醉酒成梦
醉酒成梦 2020-12-13 11:47

I\'m trying to get Ruby debugger running in one of my specs:

describe User do
  it \"should be valid\" do
    debugger
    User.new.should be_valid
  end
end         


        
6条回答
  •  别那么骄傲
    2020-12-13 12:32

    The best way I have found to debug in rSpec is by adding the following to your 'spec_helper.rb' file

    def logger
      Rails.logger
    end
    

    You can then access all the logger methods within your rSpec files and incorporate such things as tagged logging. This of course is for Rails 3 and up. If you have anything prior to Rails 3 then add this instead:

    def logger
      RAILS_DEFAULT_LOGGER
    end
    

    Once you have your logging statements in place you can enter

    tail -f log/test.log
    

    in your terminal shell in order to watch your logging statements while the tests are run.

    Of course in your actual rspec test you would enter something such as

    logger.debug "#{1.class}"  # => Fixnum
    

    If you want to filter your debug statements from the rest of your test log simply prepend a random string on to your debug statement and pipe the output of the tail command to grep.

    Example:

    logger.debug "random_string #{1.class}"   # => Fixnum
    
    tail -f log/test.log | grep random_string
    

    Update

    I've changed my opinion on this. You should install pry, pry-doc, and pry-debug, pry-debugger, and pry-rails. Then use binding.pry in your code to open an interactive debugger console that rules the world!

提交回复
热议问题