第10章 Pry, 強大的pry-rails和相關的幾個好用gem

https://asciinema.org/a/0KtCL9HB1bP08wNHLfIeOMa8K

本章講了如何定位❌,和排除bug. git

Pry  (5000🌟)github

a Ruby console. 能夠替代irb。增長了一些配合的gems,是一個傑出的debugger工具。shell

使用:在測試和開發環境使用:數據庫

gem "pry-rails"  # 取代irb ,在命令行輸入pry
json

⚠️,安裝pry時,在gem文件內輸入gem 'pry-rails', :group => :development後這樣才能取代rails console。 ruby

 

gem "pry-byebug"       session

#(文檔要求放在:test內,或者全局內)(1300+🌟) 特色:用binding.pry一步一步的除錯。app

 

gem "pry-stack_explorer"  (#350🌟) ide

#使用show-stack命令 來go up and down the stack trace工具

 

gem "pry-rescue"

 


嘗試使用pry:

 

rails console:搜索數據庫記錄,顯示更清晰,字段用不一樣顏色標記,更容易分辨。

Pry容許檢查做用域下的當前對象, 當輸入self,返回頂級做用域main.

Pry使用Unix目錄導航來讀取對象樹。 因此在Pry內輸入ls:

會返回一個目錄 是關於當前命名空間的事物:

ActiveSupport::ToJsonWithActiveSupportEncoder#methods: to_json

Rails::ConsoleMethods#methods : app  controller  helper  new_session  reload!
self.methods: inspect  to_s
locals : _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_  project

包括當前對象可使用的方法和本地變量。其中project是以前建立的變量。

 

輸入cd project就能夠進入project對象的做用域了: 

 pry(#<Project>):1>

再輸入self ,能夠顯示當前對象project的信息。

和命令終端同樣,輸入cd..返回上一級目錄,輸入cd返回最初目錄。

若是在 pry(#<Project>):1> ls 

將會返回a bunch of stuff,一大堆project能夠響應的方法(很是多,使用q加回車返回)

ls -h調用幫助信息。⚠️主要幾個:

-m, --mehtods  顯示公共方法

-M, --instance-methods   顯示定義在模塊或類中的方法

-i,   --ivars 顯示instance variables(藍色)和類變量(亮藍色) 

 


show-source(當前做用域對象的方法名) 

pry(#<Project>):1> show-source on_schedule?會顯示這個方法的具體信息
包括儲存位置。方法的定義。
show-method和show-source彷佛返回結果同樣。沒看文檔。

show-doc查看Ruby核心方法。須要安裝gem pry-doc
和irb同樣  _ 能夠顯示最近使用的結果,   _ex_顯示最近raised exception.

 


 

使用Pry 來定位測試失敗的位置 

這是gem 'pry-byebug'中的方法。

在Rspec中使用,須要require 'pry' 

使用binding.pry方法 和gem ‘byebug’的一些功能相似。 

可使用continue, finish, next, step方法。

continue,繼續執行程序直到結束

finish: 繼續執行直到當前frame結束

next: 繼續執行當前frame內的命令

step: 跳過當前,進入下一個frame。 

 

你能夠在測試任何地方插入binding.pry,你會獲得當前做用域的所有內容。 

例子:

require "rails_helper"
RSpec.describe CreatesProject do
  let(:creator) {CreatesProject.new(
    name:"Project Runway", task_string: task_string)}
  describe "initialization" do
    let(:task_string) { "" }
    it "creates a project given a name" do
      creator.build
     # creator.pry  

      binding.pry 

      expect(creator.project.name).to eq "Project Runway"
    end
  end

到了binding.pry一行,就會進入Pry,和周邊的做用域。能夠直接進行各類操做。

 

也能夠把pry發送給任何對象。好比使用creator.pry,測試時直接進入Pry,位置是本地變量creator,creator會做爲頂級做用域顯示:

CreatesProject
  initialization
[1] pry(#< CreatesProject >)>

 

這裏可使用edit命令, edit -c來打開binding.pry所定位的文件。 edit ClassName#method打開這個文件的方法。也能夠打開實例變量,具體看edit -h方法。

在你修改完成後,會重新加載文件,讓你繼續。 

 


 

加載gem 'pry-stack_explorer'

⚠️ 和pry-byebug不兼容,能夠一塊兒用,但可能出現問題。 

pry-stack_explorer是一個插件,能夠用於Pry REPL模式的開發, 讓使用者能夠在call-stack上進行導航。用戶能夠上下移動,檢查state 甚至求代碼的值evaluate code.

支持(點擊查看文檔): updownframe and show-stack

 

視頻1,在frame直間移動查看

https://asciinema.org/a/eJnrZNaUhTl12AVtnCG304d0V 

說明:先輸入show-stack,列出了目前爲止的程序運行路徑,包括調用了什麼方法什麼block。而後能夠根據路徑號,好比輸入frame 9,便可查看這個方法的細節。

 

視頻2,在一個caller中修改state

https://asciinema.org/a/0KtCL9HB1bP08wNHLfIeOMa8K 

說明: 輸入show-stack後,想要變動frame7的輸出。使用up 7到達7,而後修改state,會對以後的執行產生變化。


show-stack命令顯示當前的backtrack

up命令移動到父frame.接受數字或字符串做爲查詢條件 

frame命令移動到指定的frame. 

⚠️。frame up down的區別沒有弄明白 

 

 

 


 

rescue rspec 

使用這個命令進行測試,當出現錯誤的時候,Pry會當即進入這個❌的位置。

如:

rescue rspec spec/workflows/creates_project_spec.rb 

彈出一大坨:

From: /Users/chentianwei/下載的書籍/Rails 5 Test prescript 案例/basics/gatherer/spec/workflows/creates_project_spec.rb @ line 12 :
     7:   describe "initialization" do
     8:     let(:task_string) { "" }
     9:     it "creates a project given a name" do
    10:       creator.build
    11:       creator.pry
 => 12:       expect(creator.project.name).to eq "Project! Runway"
    13:     end
    14:   end
    15: #
    16:   # describe "mocking a failure" do
    17:   #   it "fails when we say it fails" do
RSpec::Expectations::ExpectationNotMetError:
expected: "Project! Runway"
     got: "Project Runway"
(compared using ==)
from /Users/chentianwei/.rvm/gems/ruby-2.5.0/gems/rspec-support-3.7.1/lib/rspec/support.rb:97:in `block in <module:Support>'
[1] pry(#<RSpec::ExampleGroups::CreatesProject::Initialization>)>

 

 

 

一塊兒使用這些工具 能給你一個很是互動式的工做流。 後期運行測試經過rescue, 掉進Pry失敗處,而後editing,而後再嘗試直到測試經過。

 ⚠️,在pry內修改後再,try-again仍然會報告❌,因此儘可能不使用。

另外rspec bisect ,bisect是一個隔離失敗測試的系列方法,沒有看。



幾點提示:

 

  1.  若是ActiveRecord models不保存,問題最大的多是對象建立失敗於驗證。由於記錄不能保存因此發現一個本應存在的記錄不在數據庫,因而引發測試失敗❌。可使用save!或create!,強制失敗後跳出❌提示。
  2. Rails會忽略沒有指定到permit方法的參數,並在log中記錄。當你想弄懂爲何方法明確調用了這個屬性但結果對象中不存在這個屬性時,考慮一下permit的參數🚫。
  3. 在集成測試中,不要忘記log in。 save_and_open_page命令讓你看到你沒有登錄。

 

 


 

Wiki


  • Source code browsing 源碼瀏覽( pry-doc gem)
  • Documentation Browsing文檔瀏覽show-doc
  • Live Help system活潑的幫助系統
  • 在editor中打開方法 (edit Class#method, edit my_method)
  • 默認符號高亮(自定義顏色安裝pry-theme gem)
  • Command shell integration (start editors, run git, and rake from within Pry)
  • Gist integration
  • 狀態導航 (cd, ls and friends)
  • Runtime invocation運行時的文件存取:  use Pry as a developer console or debugger(當前環境的綁定binding.pry, 對象的存取my_object.pry)
  • Exotic object support外來對象的支持 (BasicObject instances, IClasses, ...)
  • 靈活強大的命令系統
  • 能夠看和重放歷史
  • Many convenience commands inspired by IPython, Smalltalk and other advanced REPLs
  • A wide-range number of plugins that provide remote sessions, full debugging functionality, and more.
Pry也瞄準了取代IRB。他是一個嘗試:用REPL驅動開發Ruby語言。不是很強力,一個方向。 什麼是read–eval–print loop  見wiki

 

Pry提供了靈活的用戶客製化。 

pry --help


 


Editor中的使用。

 

配合cd, cd .., ls使用。

edit Class, edit Class#method, edit my_method均可以定位到不一樣的位置。

^X是退出的意思ctrl+x. ^表明control按鍵。 

^O是writeout寫出:保存修改的文件。 看提示而後回車就好了。

^G是幫助。

 


 Live Help System

 

輸入help則顯示因此Pry能夠用的命令。

詳細的使用 command_name --help 


 

使用gem pry-rails  能夠替代默認的rails console:

在加載了Rails console helper 以外還增長了一些有用的命令。 

 


 

Command Shell Integration (未弄明白)

 

使用 shell-mode來把當前工做目錄合併進Pry prompt並附帶上徹底的文件目錄名。具體的不懂。

相關文章
相關標籤/搜索