Rails Rspec 测试路由使用的 lib 类包含私有方法

问题描述 投票:0回答:1

Simplecov 检测到我在

lib/api_verson.rb
班上遗漏了一些测试:

class ApiVersion

  def initialize(version)
    @version = version
  end

  def matches?(request)
    versioned_accept_header?(request) || version_one?(request)
  end

  private

  def versioned_accept_header?(request)
    accept = request.headers['Accept']
    accept && accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}-v#{@version}\+json/]
  end

  def unversioned_accept_header?(request)
    accept = request.headers['Accept']
    accept.blank? || accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}/].nil?
  end

  def version_one?(request)
    @version == Rails.application.secrets.my_app_default_api_version && unversioned_accept_header?(request)
  end

end

路由文件使用此类来帮助设置 API 版本:

namespace :api, path: "", defaults: {format: :json} do

scope module: :v1, constraints: ApiVersion.new(1) do
  get '/alive', to: 'api#alive'
end

scope module: :v2, constraints: ApiVersion.new(2) do
  get '/alive', to: 'api#alive'
end

end

此设置是从 versioning_your_ap_is 移植的。

我正在尝试在这里测试 simplecov 报告为失败的方法:

require 'spec_helper'

describe ApiVersion do

  before(:each) do
    @apiversion = ApiVersion.new(1)
    @request = ActionController::TestRequest.new(host: 'localhost')
    @request.headers["Accept"] = "application/vnd.#{Rails.application.secrets.my_app_accept_header}-#{Rails.application.secrets.my_app_default_api_version}+json"
  end

  describe 'Method #versioned_accept_header =>' do
    it 'Should return the correct accept header version' do

binding.pry


    end
  end

end

我正在尝试构建第一个测试来尝试???到

@apiversion.send(:unversioned_accept_header, @request)
,我收到错误:

@apiversion.send(:unversioned_accept_header, @request)
NoMethodError: undefined method `unversioned_accept_header' for #<ApiVersion:0x007fae009bdad8 @version=1>
from (pry):1:in `block (3 levels) in <top (required)>'

基本上标记了以下方法:“matches?、versioned_accept_header?、unversioned_accept_header?和version_one?”

我不是 Rspec 专家。

顺便说一下,这是一个正在运行的 Rails 4 应用程序:

group :development, :test do
    gem 'pry'
    gem 'pry-doc'
    gem 'pry-debugger'
    gem 'pry-rails'
    gem 'pry-plus'
    gem 'pry-rescue'
    gem 'pry-stack_explorer'
    gem 'pry-clipboard'
    gem 'pry-nav'
  gem 'rspec-rails'
    gem 'factory_girl_rails'
    gem 'faker'
    gem 'seedbank'
    gem 'capybara'
end

group :test do
    gem 'simplecov', '~> 0.7.1'
    gem 'shoulda-matchers'
    gem 'spork-rails'
  gem 'database_cleaner'
  gem 'email_spec'
  gem 'timecop'
    gem 'json_spec'
end
ruby-on-rails ruby rspec simplecov
1个回答
1
投票

您运行此代码

@apiversion.send(:unversioned_accept_header, @request)

但是你有方法:

def unversioned_accept_header?(request)
  accept = request.headers['Accept']
  accept.blank? || accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}/].nil?
end

尝试改变

@apiversion.send(:unversioned_accept_header, @request) -> @apiversion.send(:unversioned_accept_header?, @request)

或更改方法名称

def unversioned_accept_header? -> def unversioned_accept_header
© www.soinside.com 2019 - 2024. All rights reserved.