在RSpec(精炼主题)中定义当前范围的主题时,如何访问父范围的主题?
示例代码:
describe MyModule.method(:some_method) do
context "when called with a String" do
let(:string) { "Hey there!" }
# I want to refine the subject using the parent scope's subject - common case
# is applying a subject method. Something like:
subject { super.subject.call string }
# Use subject...
end # when called with a String
end # MyModule.some_method
答案 0 :(得分:0)
好的,从上面的评论中归功于@mudasobwa,这是解决方案:
你需要使用明确地调用super()
没有参数 - super
单独行不通,因为它试图使用Ruby的隐式传递参数而失败。
更正示例:
describe MyModule.method(:some_method) do
context "when called with a String" do
let(:string) { "Hey there!" }
# Note the explicit `()`:
subject { super().call string }
# Use subject...
end # when called with a String
end # MyModule.some_method
答案 1 :(得分:0)
你可以为你的主题命名:
context do
subject(:my_thing) { described_class.new }
context '#do_stuff'
subject { my_thing.do_stuff }
it { expect(subject).to ... } # or
it { is_expected.to ... }
end
end