I keep getting errors with RSpec on Github Actions. Not sure what's going on. I'm on Rails 7 and Ruby 3.
Locally Rspec runs fine. I'm new to Github actions and have been fumbling around with it for a few hours now. It seems like an environment issue where Rails isn't loaded correctly but I'm not sure where the problem could be. Does anyone have a working Github actions example with Rails 7, Ruby 3 and RSpec?
Here's my github actions config:
# 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
Output:
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.
Fiddled around a lot with the github actions config. No luck. Always getting errors.
Turned out to be a Rails issue not Github actions. Weird that it only happens on linux and not on osx.
Fixed it by adding require 'active_model/validations'
to my application_controller.rb
file. Not sure why it's not already required automatically.
If anyone can shed some light on this I'd appreciate it.
For reference this is where the class is defined: https://github.com/rails/rails/blob/8015c2c2cf5c8718449677570f372ceb01318a32/activemodel/lib/active_model/validations.rb#L425
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.