参数数量错误(Sinatra - rspec)

时间:2018-04-12 03:53:27

标签: ruby rspec sinatra

我有一个 Sinatra 应用程序(尝试使用rspec学习基本测试)并想为此方法编写测试:

.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

我得到的错误是

{{1}}

该方法本身似乎在应用程序中运行良好,但我想编写一个仅用于学习目的的测试。

起初我试过了:

{{1}}

但我仍然遇到同样的错误。

如何让这个测试通过?

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

从规范来看,由于get已被处理,您无法访问请求,但您可以访问last_request。我会改变代码有点像这样

application_controller.rb

theta

测试

d

然后,构建您的控制器,您将调用方法,如: active_page?(请求,“/ home”)