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



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

这是错误代码

在此处输入图像描述

编码到错误代码圈CI不读取凭据.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
});
});

应通过其 Web 应用程序将RAILS_MASTER_KEY添加到项目的环境变量中。 否则,您需要将其直接粘贴到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。

最新更新