首页 > 解决方案 > 在测试期间收到 ArgumentError,但在开发中没有,为什么?

问题描述

更新:更新操作现在正在作为 JS 处理(感谢选项“xhr:true”),但是错误消息仍然存在......我不知道为什么它在测试期间抛出错误,但不是在运行时发展。

我更新了问题的标题和内容以反映这些变化。

所以现在我很茫然是什么可能导致这个错误......有什么想法吗?


每当我尝试测试控制器的更新操作时,我都会收到以下错误消息:

ArgumentError: wrong number of arguments (given 2, expected 0)

这是控制器中收到错误的位置:

def update 
  ...
  # \/ This line causing the error \/
  if @image.update(image_params.except(:image_tags_attributes))
    if image_params[:crop_x].present?
    ...

这是我正在运行的测试:

class ImagesControllerTest < ActionDispatch::IntegrationTest
  # ....
  test "should add current_user.id to tag related through table after an update action that contains crop parameters" do
    log_in_as(@user)
    patch image_path(@image), params: { image: {
                                          crop_x: 100,
                                          crop_y: 100,
                                          crop_w: 100,
                                          crop_h: 100,
                                          crop_rotation_angle: 0,
                                          image_tags_attributes: {
                                            tag_title_attributes: {
                                              title: @tag_title.title
                                            },
                                            tag_content_attributes: {
                                              content: ""
                                            }
                                          }
                                        }
                                      }, xhr: true
    assert ImageTagTitle.last.user_id == @user.id
  end
end

据我所知,我以与开发方面相同的方式将所有内容传递给测试,所以我很困惑可能导致它们处理方式不同的原因。

提前感谢您提供的任何帮助和见解!

测试日志

Processing by ImagesController#update as JS
  Parameters: {"image"=>{"crop_x"=>"100", "crop_y"=>"100", "crop_w"=>"100", "crop_h"=>"100", "crop_rotation_angle"=>"0", "image_tags_attributes"=>{"tag_title_attributes"=>{"title"=>"MyTitle1"}, "tag_content_attributes"=>{"content"=>""}}}, "id"=>"249799255"}

开发日志

Processing by ImagesController#update as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IUXYLsxSoD/OYdnm5TH/3jNbaG+7bkwZJL39p/vePkEXrnNk3J+1nrFkgIT3rsQeFmzRdt5kRMO+Vpjo7F5BCg==", "image"=>{"image_tags_attributes"=>{"tag_title_attributes"=>{"title"=>"page number"}, "tag_content_attributes"=>{"content"=>""}}, "crop_x"=>"42.73287586330139", "crop_y"=>"45.58570767432143", "crop_w"=>"123.71134020618558", "crop_h"=>"117.08396046432024", "crop_rotation_angle"=>"0"}, "button"=>"", "id"=>"2"}

标签: javascriptruby-on-railstestingminitest

解决方案


推荐阅读