Rspec测试在Circle CI(Ruby on Rails 5.2.3)上失败了

问题描述 投票:0回答:1

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

对错误代码Circle CI进行编码时,它不读取certificate.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会建议您使用字词数据。这是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个回答
0
投票

RAILS_MASTER_KEY应该通过其Web应用程序添加到项目的环境变量中。否则,您需要将其直接粘贴到docker-compose.ci.yml中,如果要将其提交到存储库中,绝对不建议这样做。

实际的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

© www.soinside.com 2019 - 2024. All rights reserved.