Search code examples
apirspecsinatrabddnameerror

RSpec - uninitialized constant for instance double


I am getting a name error for a small API i am building with RSpec, using TDD methodology

NameError:
  uninitialized constant ExpenseTracker::API::Ledger

API.rb

require "sinatra/base"
require "json"

module ExpenseTracker
  class API < Sinatra::Base

def initialize(ledger: Ledger.new) # default value so callers can just say API.new and get the default
  @ledger = ledger
  super() # rest of initialization from Sinatra
end

# Later, callers do this ledger: Ledger.new
app = API.new()

post '/expenses' do
  JSON.generate('expense_id' => 42)
end

get '/expenses/:date' do
  JSON.generate([])
end
end
end

api_spec.rb

require_relative "../../../app/api"
require "rack/test"

module ExpenseTracker

RecordResult = Struct.new(:success?, :expense_id, :error_message)

RSpec.describe API do
include Rack::Test::Methods

def app
  API.new(ledger: ledger)
end

let(:ledger) { instance_double('ExpenseTracker::Ledger') }

describe "POST /expense" do
  context "when the expenseis successfully recorded" do
    it 'returns the expense id'do
      allow(ledger).to receive(:record)
      .with(expense)
      .and_return(RecordResult.new(true, 417, nil))

    post '/expense', JSON.generate(expense)

    parsed = JSON.parse(last_response.body)
    expect(parsed).to include('expense_id': 417)
    end
    it 'responds with a 200 (OK)'
  end

  context "when the expense fails validation" do
    it 'returns an error message'
    it 'responds with 422 (Unprocessable entity)'
  end

end
end
end

Below is the error that i am getting :

An error occurred while loading ./api_spec.rb.
Failure/Error:
  def initialize(ledger: Ledger.new) # default value so callers can just say API.new and get the default
    @ledger = ledger
    super() # rest of initialization from Sinatra
  end

NameError:
  uninitialized constant ExpenseTracker::API::Ledger

/Users/Denis/Desktop/TDD/Effective_Testing_with_Rspec3/04-acceptance-specs/expense_tracker/app/api.rb:7:in `initialize'
/Users/Denis/Desktop/TDD/Effective_Testing_with_Rspec3/04-acceptance-specs/expense_tracker/app/api.rb:13:in `<class:API>'
/Users/Denis/Desktop/TDD/Effective_Testing_with_Rspec3/04-acceptance-specs/expense_tracker/app/api.rb:5:in `<module:ExpenseTracker>'
/Users/Denis/Desktop/TDD/Effective_Testing_with_Rspec3/04-acceptance-specs/expense_tracker/app/api.rb:4:in `<top (required)>'
./api_spec.rb:1:in `require_relative'
./api_spec.rb:1:in `<top (required)>'

Am I missing something? i have tried to play around with passing the ledger: Ledger.new to app = API.new but not getting past this error.

any help or ideas would be great !


Solution

  • Ok figured this one out, after a lot of head banging on the desk.

    I had to comment out the app = API.new as this is not yet implemented, every time i would load the file in the spec it would error me out.

    # Later, callers do this ledger: Ledger.new
    app = API.new()
    

    now on to the next failing spec lol !