【问题标题】:rails rspec tests for lib class used by routes contains private methods路由使用的 lib 类的 rails rspec 测试包含私有方法
【发布时间】:2014-06-13 07:25:53
【问题描述】:

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?, and 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

【问题讨论】:

  • 我想你只是错过了方法末尾的问号:@apiversion.send(:unversioned_accept_header?, @request)
  • 就是这样。 :) 你有任何后续指针如何存根私有方法,以便我可以测试 def version_one?(请求)例如?
  • @NikitaChernov 这是后续行动吗? stackoverflow.com/questions/24200877/…

标签: ruby-on-rails ruby rspec simplecov


【解决方案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

【讨论】:

  • 谢谢你完美的工作。它缺少 ?。您是否有任何后续指针如何存根私有方法以便我可以测试 def version_one?(请求)例如?
  • @apiversion.stub(:unversioned_accept_header?).and_return(true)@apiversion.stub(:unversioned_accept_header?).and_return(false) 应该做的工作
  • @NikitaChernov 我们同时发布...如果您想将其发布在我的新 ? 上,我很乐意为您提供信用。感谢您的帮助。
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多