Kris
Kris

Reputation: 19948

When specing a namespaced class how do I stub the parent class?

# models/event.rb
class Event < ActiveRecord::Base
  # ...
end

# models/event/timeline.rb
class Event::Timeline
  # ...
end

# spec/event/timeline_spec.rb
require 'spec_helper'
require 'models/event/timeline' # <- fails since "event" is not required

describe Event::Timeline do
  it '' do
    # ...
  end
end

I do not want to require 'event' since it would mean also requiring all of its dependencies which are not nessesary for the spec.

Upvotes: 2

Views: 153

Answers (1)

Tonys
Tonys

Reputation: 3399

You use stub_const method from RSpec:

https://www.relishapp.com/rspec/rspec-mocks/v/2-13/docs/mutating-constants

but probably simpler just to use class Event; end as Marian suggested.

In response to your comment, does it need to be declared above the describe block? If not perhaps try

describe Event::Timeline do
  let(:fake_class) { Class.new }

  before do
    stub_const("Event", fake_class)
  end

  it '' do
  end
end

Upvotes: 1

Related Questions