I'm currently working on a team that requires 100% code coverage and I cannot for the life of me get hit this single line method to get my current coverage up to 100%.
I have a base controller that looks like this which multiple other controller extend from.
module Owners
module Pets
class BaseController < Owners::ApplicationController
private
def current_pet
current_owner.all_pets.find(params[:pet_id])
end
end
end
end
My spec for this controller looks like this.
require 'rails_helper'
Rails.describe Owners::Pets::BaseController, type: :controller do
routes { Owners::Engine.routes }
controller Owners::Pets::BaseController do
def index
current_pet
end
end
let(:user) { double :user, id: 1, owner: nil }
before { sign_in(user) }
before do
allow(request.env['warden']).to receive(:user).and_return user
allow(Owners::User).to receive(:find).with(user.id).and_return user
end
context 'with current owner and pet' do
let(:owner) { create :owner }
let(:pet) { create(:owner_pet, owner: owner) }
describe '#current_pet' do
before do
allow(controller).to receive(:current_pet).and_return pet
routes.append { get 'index' => 'owners/pets/base#index' }
end
it do
get :index
is_expected.to eq pet
end
end
end
end
The spec is failing with the error "No route matches {:action=>"index", :controller=>"owners/pets/base"}" Routes.append should add the proper route, correct?
Update: By moving my routes { Owners::Engine.routes }
line above the anonymous controller it no longer throws an error related to routes. However now it is comparing pet
with the actual controller class. The output is too long to paste here but it's essentially:
expected: #<Owners::Pet>
got: #<#<Class:0x007fc65c860518>:0x007fc65f83a248>
with a whole bunch of attributes and methods.