Rails Rspec错误的Github操作



我一直在Github Actions上获得RSpec错误。不知道发生了什么。我正在使用Rails 7和Ruby 3。

本地Rspec运行良好。我是Github行动的新手,现在已经摸索了几个小时了。这似乎是一个环境问题,Rails没有正确加载,但我不确定问题在哪里。是否有人有一个工作的Github与Rails 7, Ruby 3和RSpec的动作示例?

下面是我的github actions配置:

# This workflow uses actions that are not certified by GitHub.  They are
# provided by a third-party and are governed by separate terms of service,
# privacy policy, and support documentation.
#
# This workflow will install a prebuilt Ruby version, install dependencies, and
# run tests and linters.
name: "Ruby on Rails CI"
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14-alpine
ports:
- "5432:5432"
env:
POSTGRES_DB: starter_rails_api_test
POSTGRES_USER: rails
POSTGRES_PASSWORD: password
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
RAILS_ENV: test
DATABASE_URL: "postgres://rails:password@localhost:5432/starter_rails_api_test"
steps:
- name: Checkout code
uses: actions/checkout@v3
# Add or replace dependency steps here
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
bundler: default
# bundler-cache: true
- name: Install gems
run: bin/bundle install
# Add or replace database setup steps here
- name: Create database tables
run: bin/rails db:create
- name: Set up database schema
run: bin/rails db:schema:load
# Add or replace test runners here
- name: Run tests
run: bin/bundle exec rspec spec
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
# Add or replace any other lints here
# - name: Security audit dependencies
#   run: bin/bundler-audit --update
# - name: Security audit application code
#   run: bin/brakeman -q -w2
- name: Lint Ruby files
run: bin/bundle exec rubocop --parallel

输出:

Run bin/bundle exec rspec spec

An error occurred while loading ./spec/models/widget_spec.rb.
Failure/Error: rescue_from ActiveModel::ValidationError, with: ->(e) { handle_validation_error(e) }
NameError:
uninitialized constant ActiveModel::ValidationError
rescue_from ActiveModel::ValidationError, with: ->(e) { handle_validation_error(e) }
^^^^^^^^^^^^^^^^^
Did you mean?  ActiveModel::Validator
# ./app/controllers/application_controller.rb:21:in `<class:ApplicationController>'
# ./app/controllers/application_controller.rb:3:in `<top (required)>'
# ./config/environment.rb:7:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `require'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/models/widget_spec.rb:3:in `require'
# ./spec/models/widget_spec.rb:3:in `<top (required)>'
An error occurred while loading ./spec/requests/api/v1/widgets_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)
FrozenError:
can't modify frozen Array: ["/home/runner/work/starter-rails-api/starter-rails-api/app/controllers", "/home/runner/work/starter-rails-api/starter-rails-api/app/controllers/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/lib", "/home/runner/work/starter-rails-api/starter-rails-api/app/models", "/home/runner/work/starter-rails-api/starter-rails-api/app/models/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/serializers"]
# ./config/environment.rb:7:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/requests/api/v1/widgets_spec.rb:3:in `<top (required)>'
An error occurred while loading ./spec/routing/api/v1/widgets_routing_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)
FrozenError:
can't modify frozen Array: ["/home/runner/work/starter-rails-api/starter-rails-api/app/controllers", "/home/runner/work/starter-rails-api/starter-rails-api/app/controllers/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/lib", "/home/runner/work/starter-rails-api/starter-rails-api/app/models", "/home/runner/work/starter-rails-api/starter-rails-api/app/models/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/serializers"]
# ./config/environment.rb:7:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/routing/api/v1/widgets_routing_spec.rb:3:in `<top (required)>'
Finished in 0.00006 seconds (files took 1.3 seconds to load)
0 examples, 0 failures, 3 errors occurred outside of examples
Error: Process completed with exit code 1.

对github actions配置进行了大量修改。没有运气。总是出错

原来是一个Rails问题不是Github的行动。奇怪的是,它只发生在linux上,而不是在osx上。

通过将require 'active_model/validations'添加到我的application_controller.rb文件来修复它。不知道为什么它不是自动需要的。

如果有人能给我一些启示,我将不胜感激。

作为参考,这是类定义的地方:https://github.com/rails/rails/blob/8015c2c2cf5c8718449677570f372ceb01318a32/activemodel/lib/active_model/validations.rb L425

最新更新