2
# 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'因为这意味着还需要它的所有依赖项,这些依赖项对于规范来说不是必需的。

4

1 回答 1

1

您使用stub_constRSpec 中的方法:

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

但可能更简单地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
于 2013-04-18T12:03:10.277 回答