Running ruby debug in rspec?

后端 未结 6 1357
醉酒成梦
醉酒成梦 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:27

    The best and cleanest option is to use --require in your .rspec file. What you put depends on which gem you use for debugging.

    --color
    --require pry
    --require rails_helper
    

    These correspond to command line options (-d or --debug is now deprecated).

    Feel free to use debugger, ruby-debug or pry (pry-rails in your Gemfile).

    For your Gemfile:

    group :test, :development do
      gem 'pry-rails'
    end
    

    Putting require 'ruby-debug' etc. at the top of your spec is simply more tightly coupled -- especially since here the top voted comment suggests putting it individually in ALL your files. With the new .rspec file you shouldn't need to put require 'spec_helper' or require 'rails_helper' at the top of your files anymore.

    They make more sense as implicit command line arguments.

    0 讨论(0)
  • 2020-12-13 12:30

    For ruby 2.0 I use byebug: https://github.com/deivid-rodriguez/byebug

    gem 'byebug'
    

    Code:

    # spec/models/user_spec.rb
    require 'spec_helper'
    require 'byebug'
    
    describe User do
      it "should be valid" do
        byebug
        User.new.should be_valid
      end
    end
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-13 12:33

    You will get what you want by including require 'ruby-debug' at the top of your spec:

    # spec/models/user_spec.rb
    require 'spec_helper'
    require 'ruby-debug'
    
    describe User do
      it "should be valid" do
        debugger
        User.new.should be_valid
      end
    end
    

    You would then run rake spec or rspec as normal

    NOTE: I now prefer Ruby 2.0+ and pry. It is pretty much the same process:

    require 'spec_helper'
    require 'pry-debugger'
    
    describe User do
      it "should be valid" do
        binding.pry
        expect(User.new).to be_valid
      end
    end
    

    Also, I generally put requires like this in my spec_helper file, so that pry-debugger is available to all of my specs.

    0 讨论(0)
  • 2020-12-13 12:35

    You can create an .rspec configuration file in the root of your project and include the line:

    --debug
    
    0 讨论(0)
  • 2020-12-13 12:38

    For Ruby >= 1.9.2

    You should install the debugger gem instead of ruby-debug19. It you use bundler, you just put this in your Gemfile:

    group :test do
      gem "debugger"
    end
    

    After that you can just put

    rspec < 3.0

    --debug
    

    rspec >= 3.0

    -rdebugger
    

    in your .rspec file

    Then you can just run

    bundle exec rake spec
    

    without any additional arguments. There is no need to modify your source code either (not even your test source code)

    0 讨论(0)
提交回复
热议问题