SImplecov - Grape 的测试覆盖率不正确 API
SImplecov - Incorrect test coverage for Grape API
我有一个 Rails 4.2 应用程序和一个基于 Grape 的 API。我开始使用 Rpsec 为它编写测试。我的测试效果很好,测试了我的预期。但是当我在终端 运行 rspec
时,Simplecov 没有显示 api 文件的正确覆盖范围,如下图所示。
目录 /lib/api/app
中的文件确实有 一些覆盖范围。但是 Simplecov 将它们显示为 0% 覆盖。
为了比较,我 运行 使用内置覆盖工具在 RubyMine 中进行规范,它显示了正确的覆盖:
那么,我是不是漏掉了什么? 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
,最开始的地方。
我有一个 Rails 4.2 应用程序和一个基于 Grape 的 API。我开始使用 Rpsec 为它编写测试。我的测试效果很好,测试了我的预期。但是当我在终端 运行 rspec
时,Simplecov 没有显示 api 文件的正确覆盖范围,如下图所示。
目录 /lib/api/app
中的文件确实有 一些覆盖范围。但是 Simplecov 将它们显示为 0% 覆盖。
为了比较,我 运行 使用内置覆盖工具在 RubyMine 中进行规范,它显示了正确的覆盖:
那么,我是不是漏掉了什么? 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
,最开始的地方。