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)>'

基本上标记了以下方法:“匹配?,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
4

1 回答 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
于 2014-06-13T07:42:32.377 回答