【问题标题】:undefined method `file_fixture_path' after upgrade to ruby 3 and rails 6.1升级到 ruby​​ 3 和 rails 6.1 后未定义的方法“file_fixture_path”
【发布时间】:2021-04-14 20:56:54
【问题描述】:

升级到 ruby​​ 3 和 rails 6.1 后,我的测试中断了

subject.avatar.attach(fixture_file_upload(Rails.root.join('spec', 'fixtures', 'images', 'avatar.jpg')))

与:

NoMethodError:
        undefined method `file_fixture_path' for RSpec::Rails::FixtureFileUploadSupport::RailsFixtureFileWrapper:Class
        Did you mean?  fixture_path

错误堆栈指向 webmock-3.11.0/lib/webmock/rspec.rb:37

有什么调试方法的建议吗?

【问题讨论】:

  • 更改为 file_fixture 后它工作得很好relishapp.com/rspec/rspec-rails/v/3-8/docs/file-fixture
  • 感谢您的信息!我刚刚遇到了同样的问题。您应该自己将信息写入答案,然后接受您自己的答案:)
  • 有同样的问题,但FactoryBot::Syntax::Default::DSL:Class。这就是为什么所有提出的解决方案都不适合我的原因。只需将 fixture_file_upload 替换为 Rack::Test::UploadFile.new('file_path', mime_type) 我知道这不是最好的解决方案。但我认为actionpack-6.1.3/lib/action_dispatch/testing/test_process.rb:43 中存在一些问题。他们应该在条件中添加self.class.respond_to?(:file_fixture_path)
  • @PavelKalashnikov 这是唯一适合我的解决方案。你知道为什么这是对的吗?
  • @Qwertie 我已经探索了动作包的来源。

标签: ruby-on-rails ruby rspec ruby-on-rails-6.1 ruby-3


【解决方案1】:

上面的任何方法都不适合我,但我找到了另一种解决方案。

在工厂里改了这个:

photo { fixture_file_upload(Rails.root.join('spec/support/images/test_image.png'), 'image/png') }

到这里:

photo { Rack::Test::UploadedFile.new('spec/support/images/test_image.png', 'image/png') }

但在那之后我又遇到了另一个错误:

unknown attribute 'service_name' for ActiveStorage::Blob

并用两个命令解决了这个问题:

rails active_storage:update
rails db:migrate

我希望这对任何人都有用。

【讨论】:

    【解决方案2】:

    添加以下初始化程序可解决包含ActionDispatch::TestProcess::FixtureFile 模块的潜在副作用的问题。

    # config/initializers/rspec.rb
    
    module RSpec
      module Rails
        module FixtureFileUploadSupport
          class RailsFixtureFileWrapper
            class << self
              attr_accessor :file_fixture_path
            end
          end
        end
      end
    end
    

    这就是 RSpec 维护者实际上是fixed 的问题所在。截至发文之日,该补丁尚未发布。

    【讨论】:

    【解决方案3】:

    遇到同样的错误,但由于请求规范中的帖子不接受 file_fixture 返回的对象,因此必须以不同的方式解决它。

    在我的请求中包含include ActionDispatch::TestProcess::FixtureFile 为我解决了这个问题。

    RSpec.describe "Attachments", type: :request do
      include Rack::Test::Methods
      include ActionDispatch::TestProcess::FixtureFile
      #...
        expect {
          file = fixture_file_upload("image.jpg", "image/jpeg", :binary)
          post collection_work_attachments_path(collection, work), {attachment: {file: file, name: image_name, visibility: [:admin]}}
        }.to change(Attachment, :count).by(1)
      #...
    end
    

    【讨论】:

    • 嗯,有趣。感谢您的解决方案。
    【解决方案4】:

    更改为 file_fixture 后,它工作得很好 relishapp.com/rspec/rspec-rails/v/3-8/docs/file-fixture

    【讨论】:

    • 没有为我工作 undefined method file_fixture' 对于#<:syntaxrunner>
    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多