簡體   English   中英

參數數量錯誤(Sinatra-rspec)

[英]Wrong number of arguments (Sinatra - rspec)

我有一個Sinatra應用程序(試圖學習使用rspec的基本測試),並希望為此方法編寫測試:

 # application_controller.rb
def active_page?(path)
  request.path_info == path
end

我的測試是

require File.expand_path '../../spec_helper.rb', __FILE__

describe "My Application Controller" do
  it "should validate the current page path" do
    get "/home"
    active_page?("/home").should eq true
  end
end

我得到的錯誤是

.F

Failures:

1) My Application Controller should validate the current page path
 Failure/Error: expect(active_page?("/home")).to be true

 ArgumentError:
   wrong number of arguments (given 0, expected 1..2)
 # /Users/deepthought/.rvm/gems/ruby-2.4.2/gems/rack-test-1.0.0/lib/rack/test.rb:116:in `request'
 # ./controllers/application_controller.rb:15:in `active_page?'
 # ./spec/controllers/application_controller_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.02004 seconds (files took 0.45736 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/application_controller_spec.rb:10 # My Application Controller should validate the current page path

該方法本身似乎可以在應用程序中很好地工作,但是我想編寫一個僅用於學習目的的測試。

一開始我嘗試過:

expect(active_page?("/home")).to eq true

但是我仍然遇到同樣的錯誤。

我如何通過考試?

嘗試這個:

describe ApplicationController do
  it "should validate the current page path" do
    get "home"
    expect(controller.active_page?("/home")).to be true
  end
end

從規范來看,由於已處理get,因此您無法訪問請求,但可以訪問last_request。 我會像這樣更改代碼

application_controller.rb

def active_page?(requested, path)
  requested.path_info == path
end

測試

require File.expand_path '../../spec_helper.rb', __FILE__

describe "My Application Controller" do
  it "should validate the current page path" do
    get "/home"
    active_page?(last_request, "/home").should eq true
  end
end

然后,形成控制器,您將調用以下方法:active_page?(請求,“ / home”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM