Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails SImplecov-葡萄API的测试覆盖率不正确_Ruby On Rails_Rspec_Grape Api_Simplecov - Fatal编程技术网

Ruby on rails SImplecov-葡萄API的测试覆盖率不正确

Ruby on rails SImplecov-葡萄API的测试覆盖率不正确,ruby-on-rails,rspec,grape-api,simplecov,Ruby On Rails,Rspec,Grape Api,Simplecov,我有一个Rails 4.2应用程序,带有基于Grape的API。我开始使用Rpsec为它编写测试。我的测试非常有效,测试了我的预期。但是,当我在终端运行rspec时,Simplecov没有显示api文件的正确覆盖范围,正如您在下面的图像中所看到的那样 目录/lib/api/app上的文件确实有一些覆盖范围。但Simplecov显示它们的覆盖率为0% 为了进行比较,我在RubyMine中使用内置的覆盖率工具运行了specs,它显示了正确的覆盖率: 那么,我是不是遗漏了什么?simplecov有

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

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

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

那么,我是不是遗漏了什么?simplecov有什么问题

这是我的
rails\u 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
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\u helper.rb
上调用Simplecov。调用它的正确位置在
spec\u helper.rb
的最开始处