下面的代碼無法正常工作,但它最好的證明就是我想要達到是否可以在Rspec中訪問周圍環境的主題?
context "this context describes the class" do
subject do
# described class is actually a module here
c = Class.new.extend(described_class)
c.some_method_that_has_been_added_through_extension
c
end
# ... testing the class itself here ...
context "instances of this class" do
subject do
# this doesn't work because it introduces a endless recursion bug
# which makes perfectly sense
subject.new
end
end
end
我也試過在我與對象初始化 內上下文中使用一個局部變量,但沒有運氣。有沒有什麼方法可以在內部範圍內的主題定義中訪問外部範圍的主題?