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

时间:2016-09-28 19:52:29

标签: ruby-on-rails rspec grape-api simplecov

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

Simplecov

dir /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

1 个答案:

答案 0 :(得分:3)

所以,我想出了问题:我在rails_helper.rb上调用了Simplecov。调用它的正确位置是spec_helper.rb,最开始。