用途:RSpec Matchers 是 RSpec 期望(expectation)的构建模块,用于在测试中定义断言并规定实际值与期望值的比较方式。基本语法为 expect(actual).to matcher(expected),反向断言用 not_to 或 to_not。
主要能力:提供相等匹配器(eq、eql、equal/be)、比较匹配器(be >、be_between、be_within)、真值匹配器(be true、be_nil、be_truthy、be_falsey)、类型匹配器(be_a、be_an_instance_of、be_a_kind_of)、集合匹配器(include、contain_exactly/match_array、start_with、end_with、all、have_attributes)、字符串匹配器(match、start_with、end_with、include)、谓词匹配器(be_valid 调用 valid?,have_key 调用 has_key?)、change 匹配器、raise_error / throw_symbol 匹配器、output 匹配器、复合匹配器(and/or 组合)与可组合匹配器(匹配器作为参数嵌套)。
使用方式:在 RSpec 测试用例中直接调用上述匹配器;复合断言可用 expect(x).to be > 0 and be < 100,或用 described_as 自定义失败信息。自定义匹配器通过 RSpec::Matchers.define 定义,可用 match 块、failure_message、failure_message_when_negated 以及 chain 方法扩展。
适用场景:Ruby 项目单元测试与行为驱动开发中的断言编写,包括对象属性校验、集合内容比对、副作用(如计数变化)验证和异常捕获测试。
限制:资料未提供安装、配置、版本兼容性、收费与价格等信息,暂未提供。