CircleCi에서만 Rspec 테스트가 떨어지는 현상

개요



CircleCi 초보자입니다.

이번에는 로컬에서는 Rspec의 테스트가 성공적으로 통과하는데 CircleCi에서만 테스트에 실패해 버려 해결하는데 상당히 시간이 걸렸습니다.

이쪽은 우선 Rspec를 통하기 위해서만의 대응책으로 근본적인 해결은 되어 있지 않습니다.
우선 CI시의 Rspec을 패스하고 싶은 경우에만 적용하십시오.

또한 근본적인 해결책이 있으면 꼭 알려주세요.


무사히 해결했습니다!

환경



CircleCi 2
Rails 5.0.7
루비 2.5.1
Mysql 5.6

코드



circleci/config.yml
version: 2 # use CircleCI 2.0
jobs: # a collection of steps
  build: # runs not using Workflows must have a `build` job as entry point
    parallelism: 3 # run three instances of this job in parallel
    docker: # run the steps with Docker
      - image: circleci/ruby:2.5.1-node-browsers # ...with this image as the primary container; this is where all `steps` will run
        environment: # environment variables for primary container
          BUNDLE_JOBS: 3
          BUNDLE_RETRY: 3
          BUNDLE_PATH: vendor/bundle
          RAILS_ENV: test
      - image: circleci/mysql:5.6
        environment:
          MYSQL_ALLOW_EMPTY_PASSWORD: true
          MYSQL_ROOT_PASSWORD: ''
          MYSQL_DATABASE: app_test
          MYSQL_HOST: 127.0.0.1
          MYSQL_ROOT_HOST: '%'
          MYSQL_USER: root
    steps: # a collection of executable commands
      - checkout # special step to check out source code to working directory

      # Which version of bundler?
      - run:
          name: Which bundler?
          command: bundle -v

      # Restore bundle cache
      # Read about caching dependencies: https://circleci.com/docs/2.0/caching/
      - restore_cache:
          keys:
            - rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
            - rails-demo-bundle-v2-

      - run: # Install Ruby dependencies
          name: Bundle Install
          command: bundle check --path vendor/bundle || bundle install --deployment

      # Store bundle cache for Ruby dependencies
      - save_cache:
          key: rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle

      - run:
          name: Yarn Install
          command: yarn install --cache-folder ~/.cache/yarn

      # Store yarn / webpacker cache
      - save_cache:
          key: rails-demo-yarn-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s

      - run: bundle exec rake db:create
      - run: bundle exec rake db:schema:load

      - run:
          name: Run rspec in parallel
          command: |
            bundle exec rspec --profile 10 \
                              --format RspecJunitFormatter \
                              --out test_results/rspec.xml \
                              --format progress \
                              $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)

      # Save test results for timing analysis
      - store_test_results: # Upload test results for display in Test Summary: https://circleci.com/docs/2.0/collect-test-data/
          path: test_results
      # See https://circleci.com/docs/2.0/deployment-integrations/ for example deploy configs


이 파일로 푸시하면

오류


ActiveRecord::StatementInvalid:
       Mysql2::Error: Incorrect string value: '\xE8\xA5\xBF\xE6\x9D\x91' for column 'username' at row 1: INSERT INTO `users` (`email`, `encrypted_password`, `created_at`, `updated_at`, `username`) VALUES ('[email protected]', '$2a$04$cFA1loXQT9LSz9Y6Jw053.hnTIvgaGChi3h86KRaP2AFLNCI15AVC', '2020-03-05 15:03:31', '2020-03-05 15:03:31', '西村')

이곳은 로컬에서 bundle exec rspec을 런타임에 성공적으로 통과하지만, CI시에만 오류가 발생합니다. 이 Mysql 에러 자체는 DB의 문자 코드의 설정에 의해 일어나는 에러로, 일본어가 모두 읽히지 않고 일어나고 있는 것 같습니다. 그래서 Rspec 테스트의 더미 데이터를 작성하고 있던 FactoryBot의 설정을 모두 영어로 하면 일단 CI상에서도 테스트를 패스할 수 있었습니다. 이상, 근본적으로는 해결되어 있지 않지만 일단 대응책으로 적어 둡니다. 추가 근본적인 해결에 이르렀습니다. 예상대로 문자 코드로 인한 오류였습니다. 발생한 오류로 검색하면 대부분이 database.yml 설정이나 mysql에 연결하여 터미널에서 문자 코드를 변경하는 방법이 히트하지만, 이번 제 경우에는 문제가 schema.rb에있었습니다 . db/schema.rb create_table "comment", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t| 원래 이와 같이 디폴트의 문자 코드는 latin1 로 되어 있어, 이쪽은 database.yml 를 변경한 것만으로는 수정할 수 없습니다. 그렇다면 어떻게 변경할까요? 먼저 database.yml을 다음과 같이 변경합니다. config/database.yml adapter: mysql2 encoding: utf8 #이하 그런 다음 한 번 migrate합니다. $ bundle exec rake db:migrate 그렇다면 schema.rb는 db/schema.rb - create_table "comments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t| + create_table "comments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 이 방법으로 문자 코드를 변경할 수 있습니다. 소개 rake db:create로 db를 작성하면 문자 코드는 latin1이 설정되는 것 같습니다. 이상입니다. 도착하는 데 상당한 시간이 걸렸기 때문에 같은 오류가 발생한 사람의 도움이 되길 바랍니다.

좋은 웹페이지 즐겨찾기