Search code examples
ruby-on-railsrubyrspecsimplecov

Rails Rspec tests for lib class used by routes contains private methods


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:

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

I am trying to build this first test to attempt??? to @apiversion.send(:unversioned_accept_header, @request) and I am getting the error:

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

Basically the following methods are flagged: "matches?, versioned_accept_header?, unversioned_accept_header?, and version_one?"

I am not an expert at Rspec.

Incidentally, this is a Rails 4 application running:

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

Solution

  • You run this code

    @apiversion.send(:unversioned_accept_header, @request)
    

    but you have method:

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

    try change

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

    or change method name

    def unversioned_accept_header? -> def unversioned_accept_header