参数数量错误(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

从spec来看,由于get已经处理完毕,无法访问request,但是可以访问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")