【问题标题】:Rspec test failed on Circle CI (Ruby on Rails 5.2.3)Circle CI 上的 Rspec 测试失败(Ruby on Rails 5.2.3)
【发布时间】:2020-09-20 07:51:57
【问题描述】:

Rspec 测试在开发环境中成功,但在 Circle CI 中失败。 我不知道这种情况下的解决方案。

这是测试代码和错误代码。

require 'rails_helper'
require 'webmock'

describe SuggestsController, type: :request do
  describe 'GET data by Ajax' do
    before do
      WebMock.stub_request(
        :get, "https://...........herokuapp.com...../api/suggests"
      ).with(
        headers: { 'Authorization' => 'Bearer api123' },
        query: hash_including({ :keyword => "ru", :max_num => "3" })
      ).to_return(
        body: ['ruby', 'ruby for women', 'ruby for men'].to_json,
        status: 200,
        headers: { 'Content-Type' => 'application/json' }
      )
    end

    it 'get response200' do
      get '/potepan/suggest'
      expect(response.status).to eq(200)
    end

    it 'Getting keyword "ru" you get 3 suggesting word ' do
      get '/potepan/suggest', params: { keyword: 'ru', max_num: 3 }
      expect(JSON.parse(response.body)).to eq ['ruby', 'ruby for women', 'ruby for men']
    end
  end
end

这是控制器

require 'httpclient'

class MyApp::SuggestsController < ApplicationController
  def suggest
    client = HTTPClient.new
    uri = Rails.application.credentials.api[:suggest_url]
    header = { "Authorization" => "Bearer #{Rails.application.credentials.api[:suggest_key]}" }
    query = {
      "keyword" => params[:keyword],
      "max_num" => params[:max_num],
    }
    response = client.get(uri, query, header)

    if response.status == 200
      render json: JSON.parse(response.body)
    else
      render json: [], status: response.status
    end
  end
end

这是错误代码

enter image description here

Accoding to error code Circle CI don't read credentials.yml.enc 所以我添加了 RAILS_MASTER_KEY: ${RAILS_MASTER_KEY} 就像在 docker-compose.ci.yml 中一样

.......
    ports:
      - '3000:3000'
    environment:
      MYSQL_USERNAME: root
      MYSQL_PASSWORD: password
      MYSQL_HOST: mysql
      REDIS_URL: "redis://redis:6379"
      RAILS_MASTER_KEY: ${RAILS_MASTER_KEY}
    depends_on:
      - mysql
      - redis
    networks:
      - default
    command: bundle exec rails server -b 0.0.0.0

但失败了 当我使用 git push heroku 部署它时,我可以得到建议。所以推测通过 Ajax 获取 suggeting word 数据。 这是jQuery代码

jQuery(document).ready(function() {
  $('.form-control').autocomplete({
    source: function(request, response) {
      $.ajax({
        type: 'GET',
        url: '/MyApp/suggest',
        data: {
          keyword: request.term,
          max_num: 5
        },
        dataType: "json"
      }).then(
        function(data) {
          response(data);
        },
        function(xhr, ts, err) {
          alert('${xhr.status}ERROR: ');
          $('.form-control').off();
        }
      );
    },
    atutoFocus: true
  });
});

【问题讨论】:

    标签: jquery ruby-on-rails ruby docker circleci


    【解决方案1】:

    RAILS_MASTER_KEY 应该通过他们的网络应用添加到您项目的环境变量中。否则,您需要将其直接粘贴到 docker-compose.ci.yml,如果您将其提交到 repo,当然不建议这样做。

    实际的RAILS_MASTER_KEY 应该存储在本地的master.key 文件中。

    相关文档:https://circleci.com/docs/2.0/env-vars/#:~:text=In%20the%20CircleCI%20application%2C%20go,enter%20a%20name%20and%20value.

    【讨论】:

    • 此错误已解决。谢谢你!在docker-compose.ci.yml 中添加RAILS_MASTER_KEY: ${RAILS_MASTER_KEY} ,你不推荐吗?从安全的角度。所以我应该在 master.key 文件中添加``` RAILS_MASTER_KEY ``` 吗?
    • @ItiroWakuda 这样做很好(尽管我不确定将 RAILS_MASTER_KEY 添加到项目变量中是否有必要)。我只是说将这样的敏感字符串复制到文件中并将其提交到 repo 是违反最佳做法的。
    • 我明白了。谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2013-03-29
    相关资源
    最近更新 更多