Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an rspec matcher to confirm a class includes a library, module or gem?

Tags:

ruby

rspec

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
like image 773
Tonys Avatar asked Jun 18 '13 18:06

Tonys


People also ask

How to extend a test class in RSpec?

Alternatively you can extend the test class with your module: let(:dummy_class) { Class.new { extend ModuleToBeTested } } Using 'let' is better than using an instance variable to define the dummy class in the before(:each) When to use RSpec let()? Share Improve this answer Follow edited Jul 3 '17 at 22:03

What is an eql matcher in RSpec?

expect (message).to eq "Hello World!" The keyword eql is an RSpec “matcher”. Here, we will introduce the other types of matchers in RSpec. Matchers to test for object or value equality.

Is it possible to include in globally in RSpec?

or you can include in globally in a spec_helper.rb file require d from your spec file (s): RSpec's built-in matchers are designed to be composed, in expressions like:

How do I include a method in a spec?

This is normally done by including the method and the class in a module, which is then included in your spec: or you can include in globally in a spec_helper.rb file require d from your spec file (s): RSpec's built-in matchers are designed to be composed, in expressions like:


1 Answers

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.

like image 93
samuil Avatar answered Nov 02 '22 04:11

samuil