是否有一个rspec匹配器来确认一个类包含库,模块或gem?

Ton*_*nys 3 rspec

使用minitest发现本教程,并想知道rspec中是否存在等效的匹配器:

有趣的minitest断言

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
Run Code Online (Sandbox Code Playgroud)

sam*_*uil 8

测试类是否包含模块通常是错误的,因为您正在测试实现细节而不是预期的行为.

通过调用ancestors类可以找到包含的模块,因此您可以使用简单的include匹配器:

expect(Dish::Player.ancestors).to include(HTTParty)
Run Code Online (Sandbox Code Playgroud)

您的第二个期望应该测试:

expect(Dish::Player.base_uri).to eq 'http://api.dribbble.com'
Run Code Online (Sandbox Code Playgroud)

编辑

直到今天我还不知道类实现了<=>运算符.你可以简单地检查一下Dish::Player < HTTParty.