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
字符串
routes文件使用此类来帮助设置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?,and version_one?”
我不是Rspec的Maven。
顺便说一下,这是一个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
型
1条答案
按热度按时间ylamdve61#
你跑这个代码
字符串
但你有方法
型
尝试改变
型
或更改方法名称
型