Rspec 在 Circle CI 上的测试失败(Ruby 在 Rails 5.2.3 上)
Rspec test failed on 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
这是错误代码
enter image description here
根据错误代码圈CI不读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 获得了提示词数据。
这是 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
});
});
RAILS_MASTER_KEY
应该通过他们的网络应用添加到项目的环境变量中。否则,您需要将其直接粘贴到 docker-compose.ci.yml
中,如果您将其提交到 repo,当然不推荐这样做。
实际的 RAILS_MASTER_KEY
应该存储在本地 master.key
文件中。
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
根据错误代码圈CI不读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 获得了提示词数据。 这是 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
});
});
RAILS_MASTER_KEY
应该通过他们的网络应用添加到项目的环境变量中。否则,您需要将其直接粘贴到 docker-compose.ci.yml
中,如果您将其提交到 repo,当然不推荐这样做。
实际的 RAILS_MASTER_KEY
应该存储在本地 master.key
文件中。