質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

CI(継続的インテグレーション)

CI(継続的インテグレーション)は、アプリ開発においてビルドとテストを繰り返すことで品質改善と納期短縮を図る手法です。JenkinsやTravis CIなどの専用ツールを利用してプロセスを自動化・半自動化して効率的に実施します。

Q&A

1回答

2690閲覧

ローカルではパスするrspecがcircleciではパスしない

gozikyu

総合スコア4

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

CI(継続的インテグレーション)

CI(継続的インテグレーション)は、アプリ開発においてビルドとテストを繰り返すことで品質改善と納期短縮を図る手法です。JenkinsやTravis CIなどの専用ツールを利用してプロセスを自動化・半自動化して効率的に実施します。

0グッド

0クリップ

投稿2021/02/26 12:33

お世話になっております。
railsとreactを用いたアプリケーションを作っており、作っている過程でcircleciでのrspecについてお聞きしたいことがあり質問させていただきました。

困っていること
①ローカルでrspecを作成、実行→パス
②ローカルで作成したrspecをリモートリポジトリにプッシュした際にcircleciでrspecを実行→failed
具体的には以下の画像の通りでjsonという変数が定義されていないようです。
同様の事例がないか調査してはみたのですが、見当たらず、困っております。。。

1) Trainings POST /search correct trainings should be searched Failure/Error: expect(json[0]['menu']).to eq('スクワット') NoMethodError: undefined method `[]' for nil:NilClass

エラー画面
https://gyazo.com/dde72ad1113db1050f890c331c97e3fa

trainings_request_spec.rb

require 'rails_helper' RSpec.describe 'Trainings', type: :request do before do @user = FactoryBot.create(:user) post '/login', params: { user: { email: 'hoge@gmail.com', password: 'password' } } @training = @user.trainings.create(FactoryBot.attributes_for(:valid_training)) end describe 'GET /index' do it 'renders a successful response' do get "/trainings/#{@user.id}" expect(response).to be_successful end end describe 'POST /create' do it 'should be registered with valid params' do expect do post '/trainings', params: { training: FactoryBot.attributes_for(:valid_training) } end.to change(@user.trainings, :count).by(1) end it 'should not be registered with invalid params' do expect do post '/trainings', params: { training: FactoryBot.attributes_for(:invalid_training) } end.to change(@user.trainings, :count).by(0) end end describe 'POST /search' do it 'correct trainings should be searched' do post '/trainings/search', params: { search: FactoryBot.attributes_for(:valid_training) } json = JSON.parse(response.body) expect(json[0]['menu']).to eq('スクワット') expect(json.length).to eq(1) end describe 'DELETE /destroy' do it 'login_user should delete own trainings' do expect do delete "/trainings/#{@training.id}" end.to change(@user.trainings, :count).by(-1) end end

.circleci/config.yml

# Ruby CircleCI 2.0 configuration file # # Check https://circleci.com/docs/2.0/language-ruby/ for more details # version: 2 jobs: rubocop: docker: - image: circleci/ruby:2.6.6-node-browsers-legacy environment: RAILS_ENV: test MYSQL_HOST: 127.0.0.1 - image: circleci/mysql:8.0 command: [--default-authentication-plugin=mysql_native_password] environment: MYSQL_USER: root MYSQL_DB: myapp_test working_directory: ~/repo steps: - checkout - run: cd api && bundle install --jobs=4 --retry=3 --path vendor/bundle - run: yarn install # Rubocop - run: name: Rubocop command: cd api && bundle exec rubocop rspec: docker: - image: circleci/ruby:2.6.6-node-browsers-legacy environment: RAILS_ENV: test MYSQL_HOST: 127.0.0.1 - image: circleci/mysql:8.0 command: [--default-authentication-plugin=mysql_native_password] environment: MYSQL_USER: root MYSQL_ROOT_PASSWORD: password MYSQL_DB: myapp_test MYSQL_ROOT_HOST: "%" working_directory: ~/repo steps: - checkout - run: cd api && bundle install --jobs=4 --retry=3 --path vendor/bundle - run: yarn install - run: cd api && bundle exec rake db:create - run: cd api && bundle exec rake db:schema:load # Rspec - run: name: Rspec command: cd api && bundle exec rspec workflows: version: 2 rubocop_rspec: jobs: - rubocop - rspec: requires: - rubocop

trainings_controller.rb

require 'time' class TrainingsController < ApplicationController before_action :logged_in_user, only: %i[create destroy] before_action :correct_user, only: [:destroy] def index @training = Training.all render json: @training.where(menu: 'ベンチプレス') end def show @training = User.find(params[:id]) render json: @training.trainings end def create @training = current_user.trainings.create(training_params) render json: @training end # def update # @user = User.find(params[:id]) # @user.update(registrations_params) # render json: @user # end def search # Viewのformで取得したパラメータをモデルに渡す @training = Training.all render json: @training.where('(date LIKE ?) AND (location LIKE ?) AND (partner LIKE ?) AND (menu LIKE ?)', "%#{date_format(params[:search][:date])}%", "%#{params[:search][:location]}%", "%#{params[:search][:partner]}%", "%#{params[:search][:menu]}%") end def destroy if @training.destroy render json: { status: 200, message: 'トレーニング予定削除成功' } else render json: { status: 404, message: 'トレーニング予定削除失敗' } end end private def training_params params.require(:training).permit(:menu, :date, :location, :partner) end def correct_user @training = current_user.trainings.find_by(id: params[:id]) end def date_format(date) Time.parse(date).in_time_zone('Tokyo').strftime('%Y-%m-%d') end end

ご知見ある方いらっしゃいましたら、アドバイス頂けると幸いです。
どうぞよろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

json が未定義なら、jsonがないというエラーです [] がない ですからjson[0] が空なのでしょう。

localで通ってよそで通らないのに私の経験では可能性は3つあります

  1. commit、push していないfileがある。

 git status して 「追跡されていないファイル:」の中に影響あるのが無いかしらべる

  1. test の database に localではデータが残っていて、それがあるので通る

 RALS_ENV=test rails db:drop して RALS_ENV=test rails db:create して RALS_ENV=test rails db:migrate して試す

  1. testの順番に依存してしまうtesut内容になっている。

 circleciの結果に seedがいくつで行ったか表示されているとおもうので、
bundle exec rspec --seed 724 --fail-fast
で試す

投稿2021/02/27 03:03

winterboum

総合スコア23376

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gozikyu

2021/02/27 04:44

アドバイスありがとうございます。 1に関してはpushし忘れているファイルはなく、gitignoreを見ても特にそれらしきものは見当たりませんでした。 https://gyazo.com/0dfea9502dff2ec562724798caae07a6 2〜3の方法を試して見ましたが、ローカルではパス、circleciではエラーという状況は変わりませんでした、、、 リクエストのレスポンス内容を調べたところ、下の写真の通り、circleciではおっしゃる通りresponse.bodyが空であることがわかりました。 なぜ空になってまうのか、というところを調べております、、 https://gyazo.com/ee513ff7f11cd60d29f5aff4c87a6b4b https://gyazo.com/8b3d788abde645b7044ded19bced1375
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問