模块: RSpec::Matchers::DSL
- 包含于
- RSpec::Matchers
- 定义于
- lib/rspec/matchers/dsl.rb
概述
定义自定义匹配器 DSL。
定义于命名空间
模块: DefaultImplementations, Macros 类: Matcher
实例方法概要 收起
-
#alias_matcher(new_name, old_name, options = {}) {|String| ... } ⇒ Object
定义匹配器别名。
-
#define(name) {|Object| ... } ⇒ Object (亦称: #matcher)
定义自定义匹配器。
-
#define_negated_matcher(negated_name, base_name) {|String| ... } ⇒ Object
定义否定匹配器。
实例方法详情
#alias_matcher(new_name, old_name, options = {}) ⇒Object
定义匹配器别名。返回的匹配器的 description
将被覆盖以反映新名称的措辞,该名称将在作为复合匹配器表达式中另一个匹配器的参数传递时用于失败消息。
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rspec/matchers/dsl.rb', line 32 def alias_matcher(new_name, old_name, ={}, &description_override) description_override ||= lambda do |old_desc| old_desc.gsub(EnglishPhrasing.split_words(old_name), EnglishPhrasing.split_words(new_name)) end klass = .fetch(:klass) { AliasedMatcher } define_method(new_name) do |*args, &block| matcher = __send__(old_name, *args, &block) matcher.matcher_name = new_name if matcher.respond_to?(:matcher_name=) klass.new(matcher, description_override) end ruby2_keywords new_name if respond_to?(:ruby2_keywords, true) end |
#define(name) {|Object| ... } ⇒Object 也称为: matcher
定义自定义匹配器。
73 74 75 76 77 78 |
# File 'lib/rspec/matchers/dsl.rb', line 73 def define(name, &declarations) warn_about_block_args(name, declarations) define_method name do |*expected, &block_arg| RSpec::Matchers::DSL::Matcher.new(name, declarations, self, *expected, &block_arg) end end |
#define_negated_matcher(negated_name, base_name) {|String| ... } ⇒Object
定义否定匹配器。 返回的匹配器的 description
和 failure_message
将被覆盖以反映新名称的措辞,并且匹配逻辑将基于原始匹配器,但被否定。
61 62 63 |
# File 'lib/rspec/matchers/dsl.rb', line 61 def define_negated_matcher(negated_name, base_name, &description_override) alias_matcher(negated_name, base_name, :klass => AliasedNegatedMatcher, &description_override) end |