在指定命名空间类时,如何存根父类?

时间:2013-04-18 08:47:45

标签: ruby rspec rspec2

# 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

我不想require 'event',因为它意味着还要求所有依赖项都不符合规范。

1 个答案:

答案 0 :(得分:1)

您使用RSpec的stub_const方法:

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

但可能更简单,只需按照Marian的建议使用class Event; end

在回复您的评论时,是否需要在describe区块之上声明?如果没有,也许试试

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

  before do
    stub_const("Event", fake_class)
  end

  it '' do
  end
end