我正在将我的项目从Rails '6.0.4.8‘升级到'6.1.6.1’。这样做破坏了我的许多rspec测试(正如我所期望的)。但我似乎不明白为什么我有很多表格:
Failure/Error:
expect(log.errors.full_messages).to(include('error message'))
expected ["attribute name ^error message"] to include "error message"显然,基于测试的类型有一些细微的变化,但是我不打算在这里发布所有的50个。根据我的理解,"^“应该将字符串修剪为只包含以下所有内容(即,去掉属性名并保留错误消息)。这在更新的版本中被改变了吗?无论如何,这是在检查是否包含,而不是完全匹配,所以奇怪的是,它失败了。
以下是我更新的软件包:
-gem 'rails', '6.0.4.8'
+gem 'rails', '6.1.6.1'
-gem 'acts-as-taggable-on', '~> 6.0'
+gem 'acts-as-taggable-on', '~> 7.0'
- gem 'rspec-rails', '4.0.0.beta3'
+ gem 'rspec-rails', '4.1.2'
- actioncable (6.0.4.8)
- actionpack (= 6.0.4.8)
+ actioncable (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionmailbox (6.0.4.8)
- actionpack (= 6.0.4.8)
- activejob (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- activestorage (= 6.0.4.8)
- activesupport (= 6.0.4.8)
+ actionmailbox (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activestorage (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionmailer (6.0.4.8)
- actionpack (= 6.0.4.8)
- actionview (= 6.0.4.8)
- activejob (= 6.0.4.8)
+ actionmailer (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ actionview (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionpack (6.0.4.8)
- actionview (= 6.0.4.8)
- activesupport (= 6.0.4.8)
- rack (~> 2.0, >= 2.0.8)
+ actionpack (6.1.6.1)
+ actionview (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ rack (~> 2.0, >= 2.0.9)
- actiontext (6.0.4.8)
- actionpack (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- activestorage (= 6.0.4.8)
- activesupport (= 6.0.4.8)
+ actiontext (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activestorage (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionview (6.0.4.8)
- activesupport (= 6.0.4.8)
+ actionview (6.1.6.1)
+ activesupport (= 6.1.6.1)
- activejob (6.0.4.8)
- activesupport (= 6.0.4.8)
+ activejob (6.1.6.1)
+ activesupport (= 6.1.6.1)
- activemodel (6.0.4.8)
- activesupport (= 6.0.4.8)
- activerecord (6.0.4.8)
- activemodel (= 6.0.4.8)
- activesupport (= 6.0.4.8)
- activestorage (6.0.4.8)
- actionpack (= 6.0.4.8)
- activejob (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- marcel (~> 1.0.0)
- activesupport (6.0.4.8)
+ activemodel (6.1.6.1)
+ activesupport (= 6.1.6.1)
+ activerecord (6.1.6.1)
+ activemodel (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ activestorage (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ marcel (~> 1.0)
+ mini_mime (>= 1.1.0)
+ activesupport (6.1.6.1)
- i18n (>= 0.7, < 2)
- minitest (~> 5.1)
- tzinfo (~> 1.1)
- zeitwerk (~> 2.2, >= 2.2.2)
- acts-as-taggable-on (6.5.0)
- activerecord (>= 5.0, < 6.1)
+ i18n (>= 1.6, < 2)
+ minitest (>= 5.1)
+ tzinfo (~> 2.0)
+ zeitwerk (~> 2.3)
+ acts-as-taggable-on (7.0.0)
+ activerecord (>= 5.0, < 6.2)
- autoprefixer-rails (10.4.2.0)
+ autoprefixer-rails (10.4.7.0)
- bcrypt (3.1.17)
+ bcrypt (3.1.18)
- bootsnap (1.11.1)
+ bootsnap (1.13.0)
- capybara (3.36.0)
+ capybara (3.37.1)
- erubi (1.10.0)
- excon (0.92.3)
+ erubi (1.11.0)
+ excon (0.92.4)
- faker (2.20.0)
+ faker (2.22.0)
- faraday (1.10.0)
- faraday-em_http (~> 1.0)
- faraday-em_synchrony (~> 1.0)
- faraday-excon (~> 1.1)
- faraday-httpclient (~> 1.0)
- faraday-multipart (~> 1.0)
- faraday-net_http (~> 1.0)
- faraday-net_http_persistent (~> 1.0)
- faraday-patron (~> 1.0)
- faraday-rack (~> 1.0)
- faraday-retry (~> 1.0)
+ faraday (2.4.0)
+ faraday-net_http (~> 2.0)
- faraday-em_http (1.0.0)
- faraday-em_synchrony (1.0.0)
- faraday-excon (1.1.0)
- faraday-httpclient (1.0.1)
- faraday-multipart (1.0.3)
- multipart-post (>= 1.2, < 3)
- faraday-net_http (1.0.1)
- faraday-net_http_persistent (1.2.0)
- faraday-patron (1.0.0)
- faraday-rack (1.0.0)
- faraday-retry (1.0.3)
+ faraday-net_http (2.1.0)
+ faraday-retry (2.0.0)
+ faraday (~> 2.0)
- fog-google (1.18.0)
+ fog-google (1.19.0)
- gapic-common (0.8.0)
- faraday (~> 1.3)
+ gapic-common (0.11.1)
+ faraday (>= 1.9, < 3.a)
+ faraday-retry (>= 1.0, < 3.a)
- googleapis-common-protos (>= 1.3.11, < 2.a)
- googleapis-common-protos-types (>= 1.0.6, < 2.a)
- googleauth (>= 0.17.0, < 2.a)
+ googleapis-common-protos (>= 1.3.12, < 2.a)
+ googleapis-common-protos-types (>= 1.3.1, < 2.a)
+ googleauth (~> 1.0)
- globalize (6.1.0)
+ globalize (6.2.1)
- google-apis-compute_v1 (0.34.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-core (0.4.2)
+ google-apis-compute_v1 (0.45.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-core (0.7.0)
- google-apis-dns_v1 (0.19.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-iamcredentials_v1 (0.10.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-monitoring_v3 (0.24.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-pubsub_v1 (0.17.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-sqladmin_v1beta4 (0.27.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-storage_v1 (0.13.0)
- google-apis-core (>= 0.4, < 2.a)
+ google-apis-dns_v1 (0.23.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-iamcredentials_v1 (0.13.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-monitoring_v3 (0.32.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-pubsub_v1 (0.24.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-sqladmin_v1beta4 (0.32.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-storage_v1 (0.17.0)
+ google-apis-core (>= 0.7, < 2.a)
- google-cloud-error_reporting-v1beta1 (0.4.5)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-error_reporting-v1beta1 (0.5.0)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-functions (1.1.3)
+ google-cloud-functions (1.2.0)
- google-cloud-functions-v1 (0.6.0)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-functions-v1 (0.8.0)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-logging (2.2.2)
+ google-cloud-logging (2.3.1)
- google-cloud-logging-v2 (0.7.0)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-logging-v2 (0.8.1)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-storage (1.36.2)
+ google-cloud-storage (1.38.0)
- google-apis-storage_v1 (~> 0.1)
+ google-apis-storage_v1 (~> 0.17.0)
- google-cloud-trace (0.41.3)
+ google-cloud-trace (0.42.1)
- google-cloud-trace-v1 (0.3.5)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-trace-v1 (0.4.0)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-trace-v2 (0.3.5)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-trace-v2 (0.4.0)
+ gapic-common (>= 0.10, < 2.a)
- google-protobuf (3.20.1)
+ google-protobuf (3.21.4)
- googleapis-common-protos-types (1.3.1)
+ googleapis-common-protos-types (1.3.2)
- googleauth (1.1.3)
+ googleauth (1.2.0)
- grpc (1.45.0)
+ grpc (1.48.0)
- grpc-google-iam-v1 (1.1.0)
+ grpc-google-iam-v1 (1.1.1)
- http-cookie (1.0.4)
+ http-cookie (1.0.5)
- i18n (1.10.0)
+ i18n (1.12.0)
- jquery-rails (4.4.0)
+ jquery-rails (4.5.0)
- json (2.6.1)
- jwt (2.3.0)
+ json (2.6.2)
+ jwt (2.4.1)
- minitest (5.15.0)
+ minitest (5.16.2)
- momentjs-rails (2.29.1.1)
+ momentjs-rails (2.29.4.1)
- msgpack (1.5.1)
+ msgpack (1.5.4)
- multipart-post (2.1.1)
- mysql2 (0.5.3)
+ mysql2 (0.5.4)
- nokogiri (1.13.6)
+ nokogiri (1.13.8)
- oj (3.13.11)
+ oj (3.13.19)
- pdf-reader (2.9.2)
+ pdf-reader (2.10.0)
- phony (2.19.11)
+ phony (2.20.0)
- rack (2.2.3.1)
+ rack (2.2.4)
- rack-test (1.1.0)
- rack (>= 1.0, < 3)
- rails (6.0.4.8)
- actioncable (= 6.0.4.8)
- actionmailbox (= 6.0.4.8)
- actionmailer (= 6.0.4.8)
- actionpack (= 6.0.4.8)
- actiontext (= 6.0.4.8)
- actionview (= 6.0.4.8)
- activejob (= 6.0.4.8)
- activemodel (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- activestorage (= 6.0.4.8)
- activesupport (= 6.0.4.8)
- bundler (>= 1.3.0)
- railties (= 6.0.4.8)
+ rack-test (2.0.2)
+ rack (>= 1.3)
+ rails (6.1.6.1)
+ actioncable (= 6.1.6.1)
+ actionmailbox (= 6.1.6.1)
+ actionmailer (= 6.1.6.1)
+ actionpack (= 6.1.6.1)
+ actiontext (= 6.1.6.1)
+ actionview (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activemodel (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activestorage (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ bundler (>= 1.15.0)
+ railties (= 6.1.6.1)
- rails-i18n (7.0.3)
+ rails-i18n (7.0.5)
- railties (6.0.4.8)
- actionpack (= 6.0.4.8)
- activesupport (= 6.0.4.8)
+ railties (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- rake (>= 0.8.7)
- thor (>= 0.20.3, < 2.0)
+ rake (>= 12.2)
+ thor (~> 1.0)
- regexp_parser (2.3.1)
- representable (3.1.1)
+ regexp_parser (2.5.0)
+ representable (3.2.0)
- rmagick (4.2.5)
+ rmagick (4.2.6)
- rspec-rails (4.0.0.beta3)
+ rspec-rails (4.1.2)
- rspec-core (~> 3.8)
- rspec-expectations (~> 3.8)
- rspec-mocks (~> 3.8)
- rspec-support (~> 3.8)
+ rspec-core (~> 3.10)
+ rspec-expectations (~> 3.10)
+ rspec-mocks (~> 3.10)
+ rspec-support (~> 3.10)
- rubocop (1.28.2)
+ rubocop (1.33.0)
+ json (~> 2.3)
- rexml
- rubocop-ast (>= 1.17.0, < 2.0)
+ rexml (>= 3.2.5, < 4.0)
+ rubocop-ast (>= 1.19.1, < 2.0)
- rubocop-ast (1.17.0)
+ rubocop-ast (1.19.1)
- rubocop-performance (1.13.3)
+ rubocop-performance (1.14.3)
- rubocop-rails (2.14.2)
+ rubocop-rails (2.15.2)
- rubocop-rspec (2.10.0)
- rubocop (~> 1.19)
+ rubocop-rspec (2.12.1)
+ rubocop (~> 1.31)
- sdoc (2.3.2)
+ sdoc (2.4.0)
- selenium-webdriver (4.1.0)
+ selenium-webdriver (4.3.0)
- rubyzip (>= 1.2.2)
- shopify_api (10.0.3)
+ rubyzip (>= 1.2.2, < 3.0)
+ websocket (~> 1.0)
+ shopify_api (11.0.1)
- signet (0.16.1)
+ signet (0.17.0)
- faraday (>= 0.17.5, < 3.0)
+ faraday (>= 0.17.5, < 3.a)
- sorbet-runtime (0.5.9959)
+ sorbet-runtime (0.5.10274)
- ssrf_filter (1.0.7)
+ ssrf_filter (1.0.8)
- thread_safe (0.3.6)
- tilt (2.0.10)
+ tilt (2.0.11)
- tzinfo (1.2.10)
- thread_safe (~> 0.1)
+ tzinfo (2.0.5)
+ concurrent-ruby (~> 1.0)
- unf_ext (0.0.8.1)
- unicode-display_width (2.1.0)
+ unf_ext (0.0.8.2)
+ unicode-display_width (2.2.0)
+ websocket (1.2.9)
- zeitwerk (2.5.4)
+ zeitwerk (2.6.0)
- acts-as-taggable-on (~> 6.0)
+ acts-as-taggable-on (~> 7.0)
- rails (= 6.0.4.8)
+ rails (= 6.1.6.1)
- rspec-rails (= 4.0.0.beta3)
+ rspec-rails (= 4.1.2)如果我漏掉了什么细节就告诉我。
就像这可能是非常重要的细节:
$ bundle exec ruby --version
ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux-musl]这是:
$ cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.15.4
PRETTY_NAME="Alpine Linux v3.15"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"为了更好的衡量:
$ bundle exec rspec --version
RSpec 3.11
- rspec-core 3.11.0
- rspec-expectations 3.11.0
- rspec-mocks 3.11.1
- rspec-rails 4.1.2
- rspec-support 3.11.0现在请随便把我撕成碎片;-)
发布于 2022-09-02 04:27:37
您观察到的行为是,以^开头的错误消息在调用full_messages时省略了属性名,这从来不是默认的Rails特性。
相反,custom-err-msg或基于该插件的gems等插件被用来将这种行为添加到Rails中。其他选项是重写Rails方法以添加此功能。有关过去是如何做到这一点的更多示例,请参阅此老问题。
由于Rails 6.1中的大多数技术不再有效,因为在Rails 6.1中,错误不再是属性键和消息字符串的简单散列。这个拉请求引入了一个用Rails 6.1封装错误消息的ActiveModel::Error模型。
实现您正在寻找的行为的推荐方法是利用本地化框架并覆盖Rails在调用full_messages时使用的默认错误消息格式。这可以在不同的层次上完成,应用程序范围很广,只适用于一个模型,甚至只适用于一个特定的属性。
例如,当您想省略属性名称时,只想显示Post#title错误的错误消息,然后将以下内容添加到config/locales.en.yml中
# config/locales/en.yml
en:
activerecord:
errors:
models:
post:
attributes:
title:
format: "%{message}" # default is "#{attribute} #{message}"我建议看看这篇博客文章,它提供了大量的示例,如何根据您的需要定制错误消息。
https://stackoverflow.com/questions/73577064
复制相似问题