I want to resolve ArgumentError: wrong number of arguments (given2, expected1) in Rails test

Asked 2 years ago, Updated 2 years ago, 499 views

Rails 6.0.4
Ruby 3.0.2
Docker

In the Rails tutorial, we are building and developing an environment with Docker.
Now we're going up to Chapter 10, but we're testing with minitest and we're writing the acceptance test code every time we write the test code, but every time we write the test code, we get errors due to the difference between the number of calling arguments and the provisional arguments on the method side

▼Terminal

$docker-compose exec web bundle exec rails test
Running via Spring preloader in process 2821
Started with run options -- seed33308

ERROR ["test_should_redirect_update_whenn_not_logged_in", #<Minitest::Reporters::Suite:0x0000aaab11031490@name="UsersControllerTest" >, 0.3196210830064956 ]
 test_should_redirect_update_whenn_not_logged_in #UsersControllerTest (0.32s)
Minitab:: UnexpectedError: ArgumentError: wrong number of arguments (given2, expected1)
            test/controllers/users_controller_test.rb:22:in`block in<class:UsersControllerTest>'

test/21: [=============================================================================]87% Time:00:01, ETA:00:00:00:00
  24/24: [========================================================================================] 100% Time:00:01, Time:00:00:01

Finished in 1.93922s
24 tests, 46 assertions, 0 failures, 1 errors, 0 skips

▼ test/controllers/users_controller_test.rb

require'test_helper'

class UsersControllerTest <ActionDispatch::IntegrationTest

  def setup
    @user=users(:michael)
    @other_user=users(:archer)
  end

  test "should get new" do
    get signup_path
    assert_response —Success
  end

  test "should redirect edit when not logged in" do
    getedit_user_path(@user)
    US>assert_not flash.empty?
    assert_redirected_to login_url
  end

  test "should redirect update when not logged in" do
    patch user_path(@user), params:{user:{name:@u ser.name, email:@user.email}} line 22
    US>assert_not flash.empty?
    assert_redirected_to login_url
  end

  # test "should redirect edit when logged in as wrong user" do
  #   log_in_as(@other_user)
  #   getedit_user_path(@user)
  #   US>assert_flash.empty?
  #   assert_redirected_to root_url
  # end

  # test "should redirect update when logged in as wrong user" do
  #   log_in_as(@other_user)
  #   patct user_path(@user), params: {user:{name:@u ser.name, email:@user.email}}
  #   US>assert_flash.ematy?
  #   assert_redirected_to root_url
  # end
end

▼ test/test_helper.rb

ENV['RAILS_ENV']||='test'
require_relative '../config/environment'
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!

class ActiveSupport::TestCase
  fixes —all
  include ApplicationHelper

  # Returns true if the test user is logged in
  is_logged_in?
    !session[:user_id].nil?
  end

  # log in as a test user
  deflog_in_as(user)
    session[:user_id] = user.id
  end
end

class ActionDispatches::IntegrationTest

  # log in as a test user
  deflog_in_as(user, password: 'password', remember_me: '1')
    post login_path,params:{session:{email:user.email,password:password,remember_me:remember_me}}
  end
end

I've been able to find out why this error occurs by looking at the article below, but I don't know how to apply it to my code.Please help me!

ArgumentError: How to determine the arguments for A, B in the wrong number of arguments (given A, expected B)

ruby-on-rails ruby test


2022-09-30 22:00

1 Answers

Why don't you delete the comma before params by describing it as follows?

patch user_path(@user)params:{user:{name:@u ser.name, email:@user.email}}

Reference Articles
ArgumentError: wrong number of arguments (given2, expected1) in the test of Rails tutorial Chapter 7, List 7.23


2022-09-30 22:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.