'Is there an rspec matcher to confirm a class includes a library, module or gem?
Found this tutorial using minitest, and was wondering if there is an equivalent matcher in rspec:
Interesting minitest assertion
describe "default attributes" do
it "must include httparty methods" do
Dish::Player.must_include HTTParty
end
it "must have the base url set to the Dribble API endpoint" do
Dish::Player.base_uri.must_equal 'http://api.dribbble.com'
end
end
Solution 1:[1]
Testing if a class has included a module is generally wrong, as you are testing implementation details instead of expected behavior.
Included modules can be found by calling ancestors on the class, so you can simply use include matcher:
expect(Dish::Player.ancestors).to include(HTTParty)
Your second expectation should be tested with:
expect(Dish::Player.base_uri).to eq 'http://api.dribbble.com'
EDIT
Until today I did not know that classes implement the <=> operator. You can simply check if Dish::Player < HTTParty.
Solution 2:[2]
You could use be_kind_of it also works for included modules:
"expect(obj).to be_kind_of(type): calls obj.kind_of?(type), which returns true if type is in obj's class hierarchy or is a module and is included in a class in obj's class hierarchy." -- https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/type-matchers
it { is_expected.to be_kind_of(HTTParty) }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | de-russification |
| Solution 2 | Marcio Klepacz |
