現在、バック(Rails API)とフロント(react)を分けてSPA開発を行っており、下記がディレクトリ構成になります。
ディレクトリ構成
.
|- api(docker)
| |- .github/workflows/github-action.yml
| |- config/database.yml.ci
| |- Gemfile
| |- Gemfile.lock
| |- Dockerfile
|
|- front(docker)
| |- Dockerfile
|
|- .gitmodules(api/frontはサブモジュールで管理)
|- docker-compose.yml
今回、GitHub Actionsを使用して自動テストの実装を進める中で下記エラーに遭遇した次第です。
エラー内容
Failures:
1) Comments API POST /api/v1/travel_records/:travel_record_id/comments コメントが正しく入力されている場合 コメントが作成できること
Failure/Error:
post api_v1_travel_record_comments_path(travel_record1.id),
params: { user_id: user1.id, content: 'test_comment' }
NoMethodError:
undefined method `now' for nil:NilClass
# ./spec/requests/comments_spec.rb:11:in `block (5 levels) in <main>'
# ./spec/requests/comments_spec.rb:10:in `block (4 levels) in <main>'
2) Comments API POST /api/v1/travel_records/:travel_record_id/comments コメントが正しく入力されている場合 コメント通知レコードが作成されること
Failure/Error:
post api_v1_travel_record_comments_path(travel_record1.id),
params: { user_id: user1.id, content: 'test_comment' }
NoMethodError:
undefined method `now' for nil:NilClass
# ./spec/requests/comments_spec.rb:19:in `block (5 levels) in <main>'
# ./spec/requests/comments_spec.rb:18:in `block (4 levels) in <main>'
原因
- リクエスト時にエラー吐かれていることからフロント側の設定を記述する必要があるのではないかと考えております(モデルテストは成功しております)
エラー内容をググってみたのですが参考記事を見つけることができず、現在も調査中です。
解決方法について分かる方いらっしゃいましたらご教示いただけると幸いです。
該当ファイル
.github/workflows/github-action.yml
name: Run rspec
on:
push:
jobs:
rspec:
runs-on: ubuntu-latest
timeout-minutes: 10
services:
mysql:
image: mysql:5.7
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: password
options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 10
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Cache node modules
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-
- name: Bundler and gem install
run: |
gem install bundler
bundle install --jobs 4 --retry 3 --path vendor/bundle
- name: Yarn install
run: yarn install --check-files
- name: Database create and migrate
run: |
cp config/database.yml.ci config/database.yml
bundle exec rails db:create RAILS_ENV=test
bundle exec ridgepole --apply -E test -f db/Schemafile -c config/database.yml.ci
- name: Run rspec
env:
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
run: bundle exec rspec
config/database.yml.ci
test:
adapter: mysql2
encoding: utf8mb4
username: root
password: password
host: 127.0.0.1
database: app_test