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

标签 ruby rspec

使用 minitest 找到本教程,并想知道 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

最佳答案

测试一个类是否包含一个模块通常是错误的,因为您正在测试实现细节而不是预期的行为。
可以通过调用 ancestors 找到包含的模块在类里面,所以你可以简单地使用include匹配器:

expect(Dish::Player.ancestors).to include(HTTParty)
您的第二个期望应该通过以下方式进行测试:
expect(Dish::Player.base_uri).to eq 'http://api.dribbble.com'
编辑
直到今天我才知道类实现了 <=>运算符(operator)。您可以简单地检查 Dish::Player < HTTParty .

关于ruby - 是否有 rspec 匹配器来确认一个类是否包含库、模块或 gem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176671/

相关文章:

ruby - Ruby 无限循环(多个类)中的用户中断?

ruby - 通过 Ruby 或命令行工具设置 PDF 元数据

ruby - float 和符号如何具有直接值(value)?

ruby-on-rails - 使用 RSpec 测试该方法是否具有参数的默认值

ruby-on-rails - 使用 RSpec stub ActiveRecord 动态查找器

ruby - 我如何在 rspec for sinatra 中使用 omniauth?

ruby-on-rails - Rspec 测试在错误的环境中运行?

ruby - 使用 Rspec,为几个选项中的每一个运行测试?

ruby-on-rails - 使用 Rspec 的 Ruby 递归深度搜索

ruby - 根据请求重新加载 rails.root/app/resources/*