Rspec 在 Rails 版本更新后失败。

huangapple go评论51阅读模式
英文:

Rspec fails after Rails version update

问题

I got into a new project that hasn't been touched for a couple of years prior, the clients want me and my team to update Ruby/Rails from Rails 4 to the newest possible version without breaking the way the product works.

After an update to Rails 5 however, about a third of the Rspec tests started breaking. Most of those were easily solvable, but I can't find any information if somebody has encountered an error like that in the past.

The error itself is:

Data Transfer Results renders data transfer results details page
       Failure/Error: get "/data_transfer_results/#{dtr.id}"
       
       NoMethodError:
         undefined method 'empty?' for #<ActionDispatch::Http::Headers:0x0000559e8ac09a08>
       # ./spec/requests/data_transfer_results_spec.rb:15:in `block (2 levels) in <top (required)>'
       # ./spec/rails_helper.rb:107:in `block (3 levels) in <top (required)>'
       # ./spec/rails_helper.rb:106:in `block (2 levels) in <top (required)>'

The test that fails contains the following:

require "rails_helper"

RSpec.describe "API Monitoring", :type => :request do
  it "renders the index template" do
    sign_in create(:user, name: 'John Doe')
    get "/api_monitoring"
    expect(response).to render_template(:index)
    expect(response.body).to include('API Monitoring')
  end
end

I suspect that this might be due to some outdated dependencies or version mismatches, but am not 100% sure.

The updated Gemfile looks like this:

source 'https://rubygems.org'

gem 'pry'
gem 'aasm', '~> 5.0'
gem 'airbrake'
gem 'american_date'
gem "audited", "~> 4.8"
gem 'aws-sdk', '~> 2.0'
gem 'better_errors', '~> 2.5.1', group: :development
gem 'binding_of_caller', '~> 0.8.0', group: :development
gem 'byebug'
gem 'capistrano', '~> 3.7'
gem 'capistrano-bundler', '~> 1.4'
gem 'capistrano-passenger'
gem 'capistrano-rails', '~> 1.4'
gem 'capistrano-rbenv', '~> 2.1'
gem 'capistrano3-delayed-job', '~> 1.0'
gem 'capybara', group: [:development, :test]
gem 'capybara-screenshot', group: [:development, :test]
gem 'chosen-rails'
gem 'compass-rails', '3.1.0'
gem 'cucumber-rails', require: false, group: [:development, :test]
gem 'daemons'
gem 'database_cleaner', group: [:development, :test]
gem 'ddtrace'
gem 'delayed_job_active_record'
gem 'devise', '~> 4.7.3'
gem 'devise-async', git: 'https://github.com/mhfs/devise-async.git', ref: '177f6363a002f7ff28f1d289c8cab7ad8d9cb8c5'
gem 'devise-security'
gem 'factory_girl_rails', group: [:development, :test]
gem 'faker', group: [:development, :test, :staging]
gem 'fixedwidth', git: 'https://github.com/gristmill/fixedwidth.git', ref: '82575c2d6faa2d1aa82158147c5268f0d4f673bd'
gem 'fog-aws'
gem 'httparty'
gem 'jbuilder', '~> 2.7'
gem 'jquery-rails'
gem 'jquery-turbolinks'
gem 'launchy', group: [:development, :test]
gem 'lograge'
gem 'logstash-event'
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
gem 'net-sftp'
gem 'omniauth-azure-activedirectory'
gem 'omniauth-saml'
gem 'paperclip', '~> 6.1'
gem 'poltergeist', group: [:development, :test]

gem 'rails', '5.2.8'
gem 'rails-erd'
gem 'rspec-rails', '> 3.10', group: [:development, :test]
gem 'ruby-kafka'
gem 'sass-rails', '~> 5.0'
gem 'sdoc', '~> 1.0', group: :doc
gem 'select2-rails'
gem 'simplecov', require: false, group: :test
#gem 'psych', '5.1.0'
gem 'skylight'
#gem 'skylight' #, '~> 5.0.1'
gem 'spring', group: :development
gem 'stupidedi'
gem 'timecop', group: :test
gem 'thread_safe'
gem 'trix'
gem 'validates_timeliness'
gem 'puma', '~> 5.6'
gem 'chronic'

gem 'after_commit_everywhere', '~> 1.0'

Maybe this is some internal call what we'd have to fix ourselves or just update some of the dependencies? We are not sure, so any help will be greatly appreciated!

Please, keep in mind that this is legacy client code and almost everything in the project is at least 4-5 years old.

英文:

I got into a new project that hasn't been touched for a couple of years prior, the clients want me and my team to update Ruby/Rails from Rails 4 to the newest possible version without breaking the way the product works.

After an update to Rails 5 however, about a third of the Rspec tests started breaking. Most of those were easily solvable, but I can't find any information if somebody has encountered an error like that in the past.

The error itself is:

Data Transfer Results renders data transfer results details page
Failure/Error: get &quot;/data_transfer_results/#{dtr.id}&quot;
NoMethodError:
undefined method `empty?&#39; for #&lt;ActionDispatch::Http::Headers:0x0000559e8ac09a08&gt;
# ./spec/requests/data_transfer_results_spec.rb:15:in `block (2 levels) in &lt;top (required)&gt;&#39;
# ./spec/rails_helper.rb:107:in `block (3 levels) in &lt;top (required)&gt;&#39;
# ./spec/rails_helper.rb:106:in `block (2 levels) in &lt;top (required)&gt;&#39;

The test that fails contains the following:

require &quot;rails_helper&quot;

RSpec.describe &quot;API Monitoring&quot;, :type =&gt; :request do
  it &quot;renders the index template&quot; do
    sign_in create(:user, name: &#39;John Doe&#39;)
    get &quot;/api_monitoring&quot;
    expect(response).to render_template(:index)
    expect(response.body).to include(&#39;API Monitoring&#39;)
  end
end

I suspect that this might be due to some outdated dependencies or version mismatches, but am not 100% sure.

The updated Gemfile looks like this:

source &#39;https://rubygems.org&#39;
gem &#39;pry&#39;
gem &#39;aasm&#39;, &#39;~&gt; 5.0&#39;
gem &#39;airbrake&#39;
gem &#39;american_date&#39;
gem &quot;audited&quot;, &quot;~&gt; 4.8&quot;
gem &#39;aws-sdk&#39;, &#39;~&gt; 2.0&#39;
gem &#39;better_errors&#39;, &#39;~&gt; 2.5.1&#39;, group: :development
gem &#39;binding_of_caller&#39;, &#39;~&gt; 0.8.0&#39;, group: :development
gem &#39;byebug&#39;
gem &#39;capistrano&#39;, &#39;~&gt; 3.7&#39;
gem &#39;capistrano-bundler&#39;, &#39;~&gt; 1.4&#39;
gem &#39;capistrano-passenger&#39;
gem &#39;capistrano-rails&#39;, &#39;~&gt; 1.4&#39;
gem &#39;capistrano-rbenv&#39;, &#39;~&gt; 2.1&#39;
gem &#39;capistrano3-delayed-job&#39;, &#39;~&gt; 1.0&#39;
gem &#39;capybara&#39;, group: [:development, :test]
gem &#39;capybara-screenshot&#39;, group: [:development, :test]
gem &#39;chosen-rails&#39;
gem &#39;compass-rails&#39;, &#39;3.1.0&#39;
gem &#39;cucumber-rails&#39;, require: false, group: [:development, :test]
gem &#39;daemons&#39;
gem &#39;database_cleaner&#39;, group: [:development, :test]
gem &#39;ddtrace&#39;
gem &#39;delayed_job_active_record&#39;
gem &#39;devise&#39;, &#39;~&gt; 4.7.3&#39;
gem &#39;devise-async&#39;, git: &#39;https://github.com/mhfs/devise-async.git&#39;, ref: &#39;177f6363a002f7ff28f1d289c8cab7ad8d9cb8c5&#39;
gem &#39;devise-security&#39;
gem &#39;factory_girl_rails&#39;, group: [:development, :test]
gem &#39;faker&#39;, group: [:development, :test, :staging]
gem &#39;fixedwidth&#39;, git: &#39;https://github.com/gristmill/fixedwidth.git&#39;, ref: &#39;82575c2d6faa2d1aa82158147c5268f0d4f673bd&#39;
gem &#39;fog-aws&#39;
gem &#39;httparty&#39;
gem &#39;jbuilder&#39;, &#39;~&gt; 2.7&#39;
gem &#39;jquery-rails&#39;
gem &#39;jquery-turbolinks&#39;
gem &#39;launchy&#39;, group: [:development, :test]
gem &#39;lograge&#39;
gem &#39;logstash-event&#39;
gem &#39;mysql2&#39;, &#39;&gt;= 0.4.4&#39;, &#39;&lt; 0.6.0&#39;
gem &#39;net-sftp&#39;
gem &#39;omniauth-azure-activedirectory&#39;
gem &#39;omniauth-saml&#39;
gem &#39;paperclip&#39;, &#39;~&gt; 6.1&#39;
gem &#39;poltergeist&#39;, group: [:development, :test]
gem &#39;rails&#39;, &#39;5.2.8&#39;
gem &#39;rails-erd&#39;
gem &#39;rspec-rails&#39;, &#39;&gt; 3.10&#39;, group: [:development, :test]
gem &#39;ruby-kafka&#39;
gem &#39;sass-rails&#39;, &#39;~&gt; 5.0&#39;
gem &#39;sdoc&#39;, &#39;~&gt; 1.0&#39;, group: :doc
gem &#39;select2-rails&#39;
gem &#39;simplecov&#39;, require: false, group: :test
#gem &#39;psych&#39;, &#39;5.1.0&#39;
gem &#39;skylight&#39;
#gem &#39;skylight&#39; #, &#39;~&gt; 5.0.1&#39;
gem &#39;spring&#39;, group: :development
gem &#39;stupidedi&#39;
gem &#39;timecop&#39;, group: :test
gem &#39;thread_safe&#39;
gem &#39;trix&#39;
gem &#39;validates_timeliness&#39;
gem &#39;puma&#39;, &#39;~&gt; 5.6&#39;
gem &#39;chronic&#39;
gem &#39;after_commit_everywhere&#39;, &#39;~&gt; 1.0&#39;

Maybe this is some internal call what we'd have to fix ourselves or just update some of the dependencies? We are not sure, so any help will be greatly appreciated!

Please, keep in mind that this is legacy client code and almost everything in the project is at least 4-5 years old.

答案1

得分: 1

该项目非常古老,因此使用了一个极其老旧的依赖项,它是2011年版本的stupidedi gem的一个分支。

其中,在lib/ruby/blank.rb文件中包含一些扩展方法:

module Enumerable

  # 如果集合为空,则返回true
  #
  # @example
  #   [1,2].blank?    #=> false
  #   [].blank?       #=> false
  #
  def blank?
    empty?
  end

  def present?
    not empty?
  end
end

导致应用程序出错的是not empty?子句。

请注意,这非常过时,当前版本的gem不会执行这种愚蠢的操作。

故事的寓意:检查应用程序和相关的分支/扩展/私有库中的方法重写。

英文:

The project is very old, and as such it uses an extremely old dependency that's a fork of a 2011 version of the stupidedi gem.

In it, there are some extension methods in the lib/ruby/blank.rb file:

module Enumerable

  # True if the collection is `empty?`
  #
  # @example
  #   [1,2].blank?    #=&gt; false
  #   [].blank?       #=&gt; false
  #
  def blank?
    empty?
  end

  def present?
    not empty?
  end
end

The thing that was breaking the application (e.g. causing the error) was the not empty? clause.

Please, not that this is very outdated and the current version of the gem doesn't do such stupid things.

Moral of the story: Check for method overrides in the application and related forks/extensions/private libs.

答案2

得分: 0

rspec-rails 3.x 适用于 Rails 版本< 5.0。请升级你的 rspec-rails 版本以与兼容的版本配合使用:

  • rspec-rails 6.x 适用于 Rails 6.1 或 7.x。
  • rspec-rails 5.x 适用于 Rails 5.2 或 6.x。
  • rspec-rails 4.x 适用于 Rails 版本从 5.x 或 6.x。
  • rspec-rails 3.x 适用于早于 5.0 的 Rails 版本。
  • rspec-rails 1.x 适用于 Rails 2.x。

我并不是在说这会解决这个问题,但在你开始调试这些问题之前,确保你正在使用的 gem 具有兼容性是非常重要的,否则你将浪费很多时间。你需要验证 Gemfile 中的每个 gem 都在使用兼容的版本。

至于错误:

ActionDispatch::Http::Headers没有 .empty? 方法,在 Rails 4 或 任何后续版本 中都没有。你应该检查在 Rails 4 下是否实际通过了该规范。我怀疑它没有通过,但由于你没有指出你发布的规范中的第 15 行是哪一行,所以无法确定。

英文:

rspec-rails 3.x is for Rails versions < 5.0. Upgrade your version of rspec-rails to a compatible version:

> rspec-rails 6.x for Rails 6.1 or 7.x.
>
> rspec-rails 5.x for Rails 5.2 or 6.x.
>
> rspec-rails 4.x for Rails from 5.x or 6.x.
>
> rspec-rails 3.x for Rails earlier than 5.0.
>
> rspec-rails 1.x for Rails 2.x.

I'm not saying that this will resolve this issue but it's important that you ensure compatibility of the gems that you're using before you start debugging these issues otherwise you're going to be wasting a lot of time. You need to validate that you're using compatible versions for every single gem in the Gemfile.

As for the error:

> undefined method `empty?' for #<ActionDispatch::Http::Headers:0x0000559e8ac09a08>

It's important to note that ActionDispatch::Http::Headers does not have an .empty? method in Rails 4 or in any subsequent version. You should check that the spec actually passes under Rails 4. I suspect that it doesn't but it's impossible to tell because you've rendered the stack trace meaningless by not indicating which line is line 15 in the spec that you posted.

huangapple
  • 本文由 发表于 2023年3月31日 19:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定