加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何更改RSpec描述块的模块上下文?

发布时间:2020-12-17 02:35:15 所属栏目:百科 来源:网络整理
导读:我有一个针对多个模块级别的对象的规范.像这样的东西: describe Foo::Bar::Baz::Quux::Widget do it "should == another Widget for the same Doohickey" do doohickey = stub Foo::Bar::Baz::Quux::Widget.new(doohickey).should == Foo::Bar::Baz::Quux::
我有一个针对多个模块级别的对象的规范.像这样的东西:

describe Foo::Bar::Baz::Quux::Widget do
  it "should == another Widget for the same Doohickey" do
    doohickey = stub
    Foo::Bar::Baz::Quux::Widget.new(doohickey).should == Foo::Bar::Baz::Quux::Widget.new(doohickey)
  end

  it "should != another Widget for a different Doohickey" do
    one_doohickey = stub
    another_doohickey = stub
    Foo::Bar::Baz::Quux::Widget.new(one_doohickey).should == Foo::Bar::Baz::Quux::Widget.new(another_doohickey)
  end
end

这是很多重复,它使我看起来像是在使用一个对象
来自其他一些命名空间我想将规范的上下文设置为
富::酒吧::巴兹:: QUUX.以下工作出奇地好:

module Foo::Bar::Baz::Quux
  describe Widget do
    it "should == another Widget for the same Doohickey" do
      doohickey = stub
      Widget.new(doohickey).should == Widget.new(doohickey)
    end

    it "should != another Widget for a different Doohickey" do
      one_doohickey = stub
      another_doohickey = stub
      Widget.new(one_doohickey).should == Widget.new(another_doohickey)
    end
  end
end

只有一个问题.因为我在Rails,我依赖
ActiveSupport的依赖管理自动加载Foo :: Bar :: Baz :: Quux
模块.之前,当我提到Foo :: Bar :: Baz :: Quux :: Widget时发生了这种情况.
现在,我自己定义模块,所以模块的真正定义
永远不会加载foo / bar / baz / quux.rb.

如何在不定义的情况下更改规范的常量查找上下文
我自己的模块?

解决方法

你可以使用describe_class助手……

describe Foo::Bar::Baz::Quux::Widget do
  it "has described_class helper" do
    described_class.should == Foo::Bar::Baz::Quux::Widget
  end
end

或者,对于lol:

describe Foo::Bar::Baz::Quux::Widget do
  def Widget
    described_class
  end

  it "has described_class helper" do
    Widget.should == Foo::Bar::Baz::Quux::Widget
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读