Simplecov detected that I was missing some tests on my lib/api_verson.rb
class:
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
This class is used by the routes file to help setup api versions:
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
This setup was ported from versioning_your_ap_is.
I am trying to test the methods here that simplecov is reporting as failures, and right now I am stuck on the case where the private method contains a private method...
def version_one?(request)
@version == Rails.application.secrets.my_app_default_api_version && unversioned_accept_header?(request)
end
This is my current spec:
require 'spec_helper'
describe ApiVersion do
before(:each) do
@apiversion = ApiVersion.new(1)
@current_api_version = Rails.application.secrets.my_app_default_api_version
@request = ActionController::TestRequest.new(host: 'localhost')
@request.headers["Accept"] = "application/vnd.#{Rails.application.secrets.my_app_accept_header}-v#{@current_api_version}+json"
end
describe "Method #versioned_accept_header? =>" do
it "Should return false if the header accept variable contains application/vnd.#{Rails.application.secrets.my_app_accept_header}-v#{@current_api_version}+json" do
expect(@apiversion.send(:unversioned_accept_header?, @request)).to eq(false)
end
it "Should return true if no version is included with the header" do
request = @request
request.headers["Accept"] = nil
expect(@apiversion.send(:unversioned_accept_header?, request)).to eq(true)
end
end
describe "Method #version_one? =>" do
it "test me" do
# @apiversion.send(:unversioned_accept_header?, @request)
binding.pry
# expect(@apiversion.send(:version_one?, @request)).to eq(false)
end
end
end
How do I stub the nested private method to test the version_one private method?