在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发布于 2017-08-30 18:24:08
好的,感谢@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发布于 2017-08-30 21:18:19
你可以给你的主题命名:
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 https://stackoverflow.com/questions/45954412
复制相似问题