Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Write Our First Tests

Brent Forwood
Brent Forwood
18,999 Points

undefined method `normalize_params' for Rack::Utils:Module

It seems the "click_button" method isn't working. When I run 'bin/rspec spec/features/todo_lists/create_spec.rb' I get this error:

Failures:

  1) Creating todo lists redirects to the todo list index page on success
     Failure/Error: click_button "Create Todo list"

     NoMethodError:
       undefined method `normalize_params' for Rack::Utils:Module
     # ./spec/features/todo_lists/create_spec.rb:13:in `block (2 levels) in <top (required)>'

Finished in 1.18 seconds (files took 8.53 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_lists/create_spec.rb:6 # Creating todo lists redirects to the todo list index page on success

Here is my 'create_spec.rb' file:

require 'spec_helper'
#added per suggestion stackoverflow to fix failure "undefined method 'visit'"
require 'rails_helper'

describe "Creating todo lists" do
  it "redirects to the todo list index page on success" do
    visit "/todo_lists"
    click_link "New Todo List"
    expect(page).to have_content("New Todo List")

    fill_in "Title", with: "My Todo List"
    fill_in "Description", with: "This is what I'm doing today."
    click_button "Create Todo list"

    expect(page).to have_content("My todo list")
  end
end

I Google searched and didn't find much.

Brent Forwood
Brent Forwood
18,999 Points

I corrected fill_in "Title", with: "My Todo List" to fill_in "Title", with: "My todo list" to match the video and the line expect(page).to have_content("My todo list"). It didn't help.

5 Answers

Manoj S M
Manoj S M
13,912 Points

If you have installed capybara 2.6.0 or the latest version and RSpec 3.x the configuration has changed leading to the above error.

After installing capybara and RSpec , the spec folder generates two new files "spec_helper.rb" and "rails_helper.rb".

Generators run in RSpec 3.x will require rails_helper and not spec_helper.

So , we need to configure and include the "rails_helper.rb" in the project folder rather than the "spec_helper".

The configuration for "rails_helper.rb" is the same as the "spec_helper.rb".

If you want to Upgrading from rspec-rails-2.x to rspec-rails-3 , please refer the documentation below: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#file-type-inference-disabled

Brent Forwood
Brent Forwood
18,999 Points

Maybe I should have expounded a little more when I fixed it. For me, simply upgrading to the latest versions of everything worked. At first, I hadn't realized that using "~> x.x.x" would limit the versions of each gem within the bundle, so I went through and changed them from what the video instructed, as it is a bit outdated. Hope that makes sense.

While I didn't follow the exact instructions of the above answer, it helped me understand better what was going on.

David Chapman
David Chapman
22,023 Points

Brent and Manoj, I was able to solve the issue by simply editing the Gemfile with the following:

  1. Follow the Rspec upgrade instructions that Manoj linked to above.
  2. Change the following in the Gemfile located in the root of your app.

Find in the Gemfile:

group :development, :test do
  gem 'rspec-rails', '~> ?.?'
end

and update the version numbers to your rspec-rails version, which is 3.5 for me, but should be at least 3 (Check in console with 'gem list' command)

Find:

group :test do
    gem 'capybara', '~> ?.?.?'
end

and do the same (2.7.1 for me).

Note: I am still getting "undefined method `stub_model'" errors when running a full rake spec but will post separately.

Thanks Manoj.

Manoj S M
Manoj S M
13,912 Points

The reason for the undefined method 'stud_model' is that , according to the change log , the version 3.0 and above of the rails_spec the mock_model and the stub_model are removed from the rspec-activemodel

rspec mocks are externalized in an another gem rspec-activemodel-mocks . You should include it in your Gemfile and try it.

hopes it help u

David O' Rojo
David O' Rojo
11,050 Points

Thanks! I was having that error with a project running the latest version of RSpec with an old version of Capybara and upgrading Capybara fixed it!

David Chapman
David Chapman
22,023 Points

Hi, I'm having the same issue as Brent and have not successfully solved it. I've followed the instructions on this video series to the letter and consistently have the "normalize_params" error (always point to some form of click_button, click_link, etc.)

Versions: -Rails: 5.0.0 -Rake: 11.2.2, 10.4.2 -Capybara: 2.7.1, 2.1.0 -Rspec: 3.5.0, 3.1.0, 2.99.0 -Ruby: 2.2.4p230 (2015-12-16 revision 53155)

-I've tried all alternate syntax from here: https://gist.github.com/zhengjia/428105 -Read this discussion but the "solution" seems to be to simply upgrade Capybara: https://github.com/jnicklas/capybara/issues/1592. -I tried following Manoj's advice. There was no rails_helper.rb file, so I did a simple Save As of spec_helper.rb. This did not solve the problem.

As a side note, I also get deprecation warnings that I've attempted unsuccessfully to locate and address: -DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead -RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3.

Help much appreciated!

David Chapman
David Chapman
22,023 Points

PS: I also attempted to follow the rspec upgrade instructions at the link that Manoj provided.

Manoj S M
Manoj S M
13,912 Points

So the issue is resolved as of now right

David Chapman
David Chapman
22,023 Points

Thanks Manoj—you beat me to that response. This measure successfully eliminated all but one of my test failures.

I have one last failing test when running rake spec:

  Failure/Error: if @todo_list.update(todo_list_params)

       #<TodoList id: 1, title: "MyString", description: "My Description", created_at: "2016-08-08 15:55:30", updated_at: "2016-08-08 15:55:30"> received :update with unexpected arguments
         expected: ({"title"=>"MyString"})
              got: (<ActionController::Parameters {"title"=>"MyString"} permitted: true>)
       Diff:
       @@ -1,2 +1,2 @@
       -[{"title"=>"MyString"}]
       +[<ActionController::Parameters {"title"=>"MyString"} permitted: true>]

Thanks.

Manoj S M
Manoj S M
13,912 Points

Can you just tell me while running which file you got that error

It would be helpful for me to debug the error