gpt4 book ai didi

ruby-on-rails - SImplecov - Grape API 的测试覆盖率不正确

转载 作者:行者123 更新时间:2023-12-02 17:24:33 25 4
gpt4 key购买 nike

我有一个带有基于 Grape 的 API 的 Rails 4.2 应用程序。我开始使用 Rpsec 为它编写测试。我的测试效果很好,测试了我的预期。但是当我在终端运行 rspec 时,Simplecov 没有显示 api 文件的正确覆盖范围,如下图所示。

Simplecov

目录/lib/api/app 中的文件确实 有一些覆盖范围。但是 Simplecov 将它们显示为 0% 覆盖。

为了比较,我使用内置的覆盖工具在 RubyMine 中运行规范,它显示了正确的覆盖:

enter image description here

所以,我在这里遗漏了什么吗? simplecov 有什么问题?

这是我的 rails_helper.rb :

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'simplecov'
SimpleCov.start 'rails'

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }


ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

config.include RSpec::Rails::RequestExampleGroup, type: :request, file_path: /spec\/api\/v1/


config.fixture_path = "#{::Rails.root}/spec/fixtures"

config.use_transactional_fixtures = false


config.infer_spec_type_from_file_location!

Faker::Config.locale = 'pt-BR'

end

这是 API 端点之一,trips.rb:

module Api
module App
class Trips < Grape::API
include Grape::Kaminari
resource :trips do
desc 'Return a list of trips of a vehicle.'
params do
requires :vehicle_id, type: Integer, desc: 'id of the vehicle'
optional :page, type: Integer, desc: 'Page of registers. Default to 1 (first page).'
optional :per, type: Integer, desc: 'Number of registers per page. Default to 25.'
end
get do
vehicle = Vehicle.find params[:vehicle_id]
if vehicle.user == current_user
trips = Trip.where(vehicle_id: vehicle.id ).order(started_at: :desc)

present paginate(trips), with: Entities::Trip
else
error!('Unauthorized', 401)
end
end

desc 'Return a Trip'
params do
requires :id, type: Integer, desc: 'id of the Trip'
end

route_param :id do
get do
trip = Trip.find params[:id]
if trip.vehicle.user == current_user
present trip, with: Entities::Trip
else
error!('Unauthorized', 401)
end

end
end

end
end
end
end

这是一个应该 100% 覆盖的示例规范 (trips_spec.rb):

describe Api::App::Trips do
include ApiHelpers

let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:vehicle) { create(:vehicle, user_id: user.id) }
let(:vehicle2) { create(:vehicle, user: user2) }
let(:trip) { create(:trip, vehicle: vehicle) }
let(:trip2) { create(:trip, vehicle: vehicle2) }
let(:auth_headers) { user.create_new_auth_token }




describe 'GET /api/v1/trips/:id' do

context 'when not authenticated' do
it 'returns 401 unauthorized' do
get "/api/v1/trips/#{trip.id}"
expect(response).to have_http_status(401)
end
end

context 'when user owns the vehicle' do

it 'returns a trip by id' do
get "/api/v1/trips/#{trip.id}", nil, auth_headers
expect(response.status).to eq(200)
expect(json_response).to be_an Hash
end
end

context 'when vehicle is from another user' do

it 'returns error 404' do
get "/api/v1/trips/#{trip2.id}", nil, auth_headers
expect(response.status).to eq(401)
end
end

end

describe 'GET /api/v1/trips' do

context 'when user owns the vehicle' do

it 'returns a list of trips by vehicle id' do
get "/api/v1/trips?vehicle_id=#{vehicle.id}", nil, auth_headers
expect(response.status).to eq(200)
expect(json_response).to be_an Array
end
end

context 'when vehicle belongs to another user' do
it 'returns error 404' do
get "/api/v1/trips?vehicle_id=#{vehicle2.id}", nil, auth_headers
expect(response.status).to eq(401)
end
end

end

end

最佳答案

所以,我发现了问题所在:我在 rails_helper.rb 上调用 Simplecov。调用它的正确位置是在 spec_helper.rb 的开头。

关于ruby-on-rails - SImplecov - Grape API 的测试覆盖率不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com