How can I change the context of the RSpec description block module?

I have a specification for an object that is at several levels of modules. Something like that:

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 

This is a lot of repetition, and it makes it look like I'm using an object from a different namespace. I would like to set the specification context for Foo::Bar::Baz::Quux . The following works are surprisingly good:

 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 

There is only one problem. Since I'm in Rails, I'm depending on ActiveSupport dependency management to automatically load the Foo::Bar::Baz::Quux module. This used to happen when I mentioned Foo::Bar::Baz::Quux::Widget . Now I define the module myself, so the real module definition in foo/bar/baz/quux.rb never loads.

How to change the constant search context for my specification without defining the module itself?

+4
source share
2 answers

You can use the described_class helper ...

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

Or, for 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 
+5
source

Can you assign this to a variable?

 widget_class = Foo::Bar::Baz::Quux::Widget 

this should dry the code a bit. Just a thought.

0
source

Source: https://habr.com/ru/post/1394443/


All Articles