# 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'
,因为它意味着还要求所有依赖项都不符合规范。
答案 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