0

I have a WelcomeController#index which simply renders index.html.erb. I need to test this by checking whether a link in the page has a particular href as passing some specific query parameter can change this href.

I want to test the basic index page without query parameter.

In spec/controllers/welcome_controller_spec.rb I have,

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

But when I run rspec spec/controllers/welcome_controller_spec.rb I keep getting this error

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

I using Ruby 2.5, Rails 5 and RSpec 3

Parthan
  • 487
  • 4
  • 16
  • 1
    try to change `require 'spec_helper'` to `require 'rails_helper'`. – akbarbin Mar 22 '18 at 10:24
  • Potential duplicate of https://stackoverflow.com/questions/24145329/how-is-spec-rails-helper-rb-different-from-spec-spec-helper-rb-do-i-need-it – max Mar 22 '18 at 11:51
  • Try putting a ‘/‘ in front of the word ‘index’ in your get call. – hashrocket Mar 22 '18 at 14:18
  • Made the changes and it now gives the following error, /home/foo/.rvm/gems/ruby-2.5.0@rails5/gems/rspec-rails-3.0.2/lib/rspec/rails/fixture_support.rb:25:in `block in ': undefined method `use_transactional_fixtures=' for # (NoMethodError) Did you mean? use_transactional_tests= use_transactional_tests use_transactional_tests? – Parthan Mar 23 '18 at 11:51

1 Answers1

0

type: :controller should be passed as an option to describe method:

Rspec.describe WelcomeController, type: :controller do
  # ...
end
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91