Rails RSpec get:index railses undefined method get

时间:2018-03-22 09:59:33

标签: ruby-on-rails rspec

我有一个WelcomeController #index,只需渲染index.html.erb。我需要通过检查页面中的链接是否具有特定的href来测试这一点,因为传递一些特定的查询参数可以更改此href。

我想测试没有查询参数的基本索引页面。

在spec / controllers / welcome_controller_spec.rb中我有,

require 'spec_helper'

# describe WelcomeController, :type => :controller do
#     it "tests index page content" do
#         get :index
#         assert_response 200
#     end
# end

RSpec.describe WelcomeController, :type => :controller do
    it "should go to the index page" do
        get 'index'
        response.should render_template "welcom/index"
        response.body.should =~ /test link text/
    end
end

但是当我运行rspec spec/controllers/welcome_controller_spec.rb时,我不断收到此错误

Failures:

  1) WelcomeController should go to the index page
     Failure/Error: get 'index'
     NoMethodError:
       undefined method `get' for #<RSpec::ExampleGroups::WelcomeController:0x0000000003ac3ca0>
       Did you mean?  gets
                      gem
     # ./spec/controllers/welcome_controller_spec.rb:13:in `block (2 levels) in <top (required)>'

Finished in 0.00038 seconds (files took 1.09 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/welcome_controller_spec.rb:12 # WelcomeController should go to the index page

我使用Ruby 2.5,Rails 5和RSpec 3

1 个答案:

答案 0 :(得分:0)

type: :controller应作为describe方法的选项传递:

Rspec.describe WelcomeController, type: :controller do
  # ...
end