类型匹配器

rspec-expectations 包含两个匹配器来指定对象类型

这两个匹配器都有别名

  expect(obj).to be_a_kind_of(type)      # same as expect(obj).to be_kind_of(type)
  expect(obj).to be_a(type)              # same as expect(obj).to be_kind_of(type)
  expect(obj).to be_an(type)             # same as expect(obj).to be_kind_of(type)
  expect(obj).to be_an_instance_of(type) # same as expect(obj).to be_instance_of(type)

使用 be_(a_)kind_of 匹配器

假设一个名为“bekindofmatcherspec.rb”的文件,其中包含

module MyModule; end

class Float
  include MyModule
end

RSpec.describe 17.0 do
  # the actual class
  it { is_expected.to be_kind_of(Float) }
  it { is_expected.to be_a_kind_of(Float) }
  it { is_expected.to be_a(Float) }

  # the superclass
  it { is_expected.to be_kind_of(Numeric) }
  it { is_expected.to be_a_kind_of(Numeric) }
  it { is_expected.to be_an(Numeric) }

  # an included module
  it { is_expected.to be_kind_of(MyModule) }
  it { is_expected.to be_a_kind_of(MyModule) }
  it { is_expected.to be_a(MyModule) }

  # negative passing case
  it { is_expected.not_to be_kind_of(String) }
  it { is_expected.not_to be_a_kind_of(String) }
  it { is_expected.not_to be_a(String) }

  # deliberate failures
  it { is_expected.not_to be_kind_of(Float) }
  it { is_expected.not_to be_a_kind_of(Float) }
  it { is_expected.not_to be_a(Float) }
  it { is_expected.not_to be_kind_of(Numeric) }
  it { is_expected.not_to be_a_kind_of(Numeric) }
  it { is_expected.not_to be_an(Numeric) }
  it { is_expected.not_to be_kind_of(MyModule) }
  it { is_expected.not_to be_a_kind_of(MyModule) }
  it { is_expected.not_to be_a(MyModule) }
  it { is_expected.to be_kind_of(String) }
  it { is_expected.to be_a_kind_of(String) }
  it { is_expected.to be_a(String) }
end

我运行 rspec be_kind_of_matcher_spec.rb

那么输出应包含所有这些

24 个示例,12 个失败
预期 17.0 不属于 Float 类型
预期 17.0 不属于 Numeric 类型
预期 17.0 不属于 MyModule 类型
预期 17.0 属于 String 类型

使用 be_(an_)instance_of 匹配器

假设一个名为“beinstanceofmatcherspec.rb”的文件,其中包含

module MyModule; end

class Float
  include MyModule
end

RSpec.describe 17.0 do
  # the actual class
  it { is_expected.to be_instance_of(Float) }
  it { is_expected.to be_an_instance_of(Float) }

  # the superclass
  it { is_expected.not_to be_instance_of(Numeric) }
  it { is_expected.not_to be_an_instance_of(Numeric) }

  # an included module
  it { is_expected.not_to be_instance_of(MyModule) }
  it { is_expected.not_to be_an_instance_of(MyModule) }

  # another class with no relation to the subject's hierarchy
  it { is_expected.not_to be_instance_of(String) }
  it { is_expected.not_to be_an_instance_of(String) }

  # deliberate failures
  it { is_expected.not_to be_instance_of(Float) }
  it { is_expected.not_to be_an_instance_of(Float) }
  it { is_expected.to be_instance_of(Numeric) }
  it { is_expected.to be_an_instance_of(Numeric) }
  it { is_expected.to be_instance_of(MyModule) }
  it { is_expected.to be_an_instance_of(MyModule) }
  it { is_expected.to be_instance_of(String) }
  it { is_expected.to be_an_instance_of(String) }
end

我运行 rspec be_instance_of_matcher_spec.rb

那么输出应包含所有这些

16 个示例,8 个失败
预期 17.0 不属于 Float 实例
预期 17.0 属于 Numeric 实例
预期 17.0 属于 MyModule 实例
预期 17.0 属于 String 实例