繁体   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