Framhal
Framhal

Reputation: 21

Ruby on Rails Tutorial : Integration test error

I'm just getting started with Ruby on Rails and I already feel like an idiot being stuck on something that seems so simple.

I'm stuck on Chapter 7.3.4 of Michael Hartl's Ruby on Rails tutorial. I'm doing an Integration Test on a user signup to check for invalid form submission.

I've thoroughly followed the tutorial so far and have been able to grasp every concept or error I ran into, but this one got me stuck. Upon trying Rails t in the Console (Ubuntu), I'm getting the following error :

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

  19/19: [=================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.84116s
19 tests, 38 assertions, 0 failures, 1 errors, 0 skips

Here is the test file itself, where the error comes from :

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

Here is the User controller file :

class UsersController < ApplicationController

def show
    @user = User.find(params[:id])
end

def new
    @user = User.new
end

def create
    @user = User.new(user_params)
    if @user.save
      # Handle a successful save.
    else
      render 'new'
    end
end

private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

end

And here is my Gemfile :

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.0'

gem 'rails',      '6.1.0'
gem 'bcrypt',     '3.1.13'
gem 'bootstrap-sass', '3.4.1'
gem 'puma',       '5.0.4'
gem 'sass-rails', '6.0.0'
gem 'webpacker',  '4.2.2'
gem 'turbolinks', '5.2.1'
gem 'jbuilder',   '2.10.0'
gem 'rexml'
gem 'bootsnap',   '1.4.6', require: false

group :development, :test do
  gem 'sqlite3', '1.4.2'
  gem 'byebug',  '11.1.3', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'web-console',           '4.1.0'
  gem 'listen',                '3.4.1'
  gem 'spring',                '2.1.1'
  gem 'spring-watcher-listen', '2.0.1'
end

group :test do
  gem 'capybara',                 '3.32.2'
  gem 'selenium-webdriver',       '3.142.7'
  gem 'webdrivers',               '4.3.0'
  gem 'rails-controller-testing', '1.0.4'
  gem 'minitest',                 '5.11.3'
  gem 'minitest-reporters',       '1.3.8'
  gem 'guard',                    '2.16.2'
  gem 'guard-minitest',           '2.4.6'
  gem 'pry'
end

group :production do
  gem 'pg', '1.2.3'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
# Uncomment the following line if you're running Rails
# on a native Windows system:
# gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

I've searched for similar threads and actually found this on StackOverflow :

Chapter 7 Errors, Ruby on Rails tutorial

I do think it's the syntax of the test that must be changed, and I've tried the solution suggested on there, but the test still fails tells me about a wrong number of arguments. It must be pretty simple, although I'm just not seeing it...

Thank you guys in advance for your time and help !

Upvotes: 0

Views: 207

Answers (1)

Ryan Jones
Ryan Jones

Reputation: 79

I was receiving and was baffled by the same error. It says that on the line with the TestCase method post I was passing two arguments, while it expected just one. I'm not sure why, since the documentation for that method indicates that it does indeed take two arguments - an action and an args hash.

Anyway, I decided to just follow the logic of the error and write it so that it's only one argument being passed to post - I removed the comma between users_path and params:

assert_no_difference 'User.count' do
      post users_path params: { user: { name: "", email: "user@invalid", password: "foo", password_confirmation: "bar" } }
end

I have no idea why that works, especially as it seems to be wrong according to the documentation. But the test now passes without errors.

Upvotes: 2

Related Questions