首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否在RSpec中访问父作用域主题?

是否在RSpec中访问父作用域主题?
EN

Stack Overflow用户
提问于 2017-08-30 15:26:06
回答 2查看 822关注 0票数 1

在RSpec中定义当前作用域的主题(细化主题)时,如何访问父作用域的主题?

示例代码:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2017-08-30 18:24:08

好的,感谢@mudasobwa来自上面的评论,这是解决方案:

您需要显式地不带参数地调用super() -- super本身是行不通的,因为它试图使用Ruby隐式传递参数,但以失败告终。

更正示例:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-08-30 21:18:19

你可以给你的主题命名:

代码语言:javascript
复制
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   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45954412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档