Home > OS >  Ruby on Rails Tutorial Chapter 7 Integration Test Argument Error
Ruby on Rails Tutorial Chapter 7 Integration Test Argument Error

Time:09-17

Just starting with Rails and am working my way through Ruby on Rails Tutorial 6th edition. Things have been going great, been able to fix all issues, until now! I'm stuck on Chapter 7.3.4 while testing for Invalid Form Submissions. When running tests I receive the following:

ERROR["test_invalid_signup_information", #<Minitest::Reporters::Suite:0x00000001390c2d78 @name="UsersSignupTest">, 0.7315919999964535]
 test_invalid_signup_information#UsersSignupTest (0.73s)
Minitest::UnexpectedError:         ArgumentError: wrong number of arguments (given 2, expected 1)
            test/integration/users_signup_test.rb:8:in `block in <class:UsersSignupTest>'

  17/17: [===============================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.82379s
17 tests, 33 assertions, 0 failures, 1 errors, 0 skips

Here is the test file. I've tried testing both files and they produce the same error:

require "test_helper"

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    before_count = User.count
    post users_path, params: { user: { name: '',
                                       email: 'user@invalid',
                                       password:              'foo',
                                       password_confirmation: 'bar' } }
    after_count = User.count
    assert_equal before_count, after_count
    assert_template 'users/new'
  end
end
require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, params: { user: { name:  "",
                                         email: "user@invalid",
                                         password:              "foo",
                                         password_confirmation: "bar" } }
    end
    assert_template 'users/new'
  end
end

This is my first experience with Rails so any help would be super appreciated!

CodePudding user response:

Found a solution for this: treat the arguments to the call to post as one by removing the comma. Pretty sure it should throw a Syntax error but for some reason it doesn't and this passes all tests. New code:

equire 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path params: { user: { name:  "",
                                         email: "user@invalid",
                                         password:              "foo",
                                         password_confirmation: "bar" } }
    end
    assert_template 'users/new'
  end
end

CodePudding user response:

I have been dealing with the same issue for about two days now.

Here's what I have discovered:

  • Put the following command in the start of the test

    p method(:post)

  • Run rails test again and get the following message

    #<Method: UsersSignupTest(Rails::Controller::Testing::Integration)#post(*args) /home/<your_name>/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/rails-controller-testing-0.0.3/lib/rails/controller/testing/integration.rb:10>

    We can see here that the post method has indeed 1 Argument and the gem responsible is the rails-controller-testing.

Solution

By deleting the rails-controller-testing gem from Gemfile and run bundle install again the post method will accept 2 Arguments without an error as described in the Rails Documentation.

The problem that rises, however, is that the assert_template commands will prompt you to reinstall the gem while running rails test.

Try using assert_response or assert_select instead.

  • Related