3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Railsチュートリアル7.3.4でハマった(ArgumentError)

Last updated at Posted at 2022-03-20

内容

タイトルの通りです。ArgumentErrorのエラーが発生しました。rails7系でrailsチュートリアルに取り組んだ際の内容です。

開発環境

ruby 3.1.1
rails 7.0.0

テストコード

users_signup_test.rb
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

実行結果

$ rails t
Running 18 tests in a single process (parallelization threshold is 50)
Started with run options --seed 3639

ERROR["test_invalid_signup_information", #<Minitest::Reporters::Suite:0x000000010c979b60 @name="UsersSignupTest">, 0.13537400000495836]
 test_invalid_signup_information#UsersSignupTest (0.14s)
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>'

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

Finished in 0.17766s
18 tests, 31 assertions, 0 failures, 1 errors, 0 skips

解決策

どうやらバージョンの違いによるエラーでした。
調べたところ困っている方も多かったので解決策を書いておきます。
post users_path,〜の「,」を消せば通ります。

改善済みコード

users_signup_test.rb
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
3
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?