ruby-on-rails Github操作中的Rails Rspec错误

irtuqstp  于 2022-12-24  发布在  Ruby
关注(0)|答案(1)|浏览(161)

我在Github操作中使用RSpec时总是出错。不知道是怎么回事。我使用的是Rails7和Ruby3。
Rspec在本地运行良好。我是Github动作的新手,现在已经摸索了几个小时了。这看起来像是Rails没有正确加载的环境问题,但我不确定问题可能在哪里。有人有一个使用Rails 7、Ruby 3和Rspec的Github动作示例吗?
以下是我的github操作配置:

# 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动作配置。没有运气。总是得到错误。

u3r8eeie

u3r8eeie1#

原来是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

相关问题