ruby – 我可以为RSpec匹配器添加别名吗?
发布时间:2020-12-17 02:23:02 所属栏目:百科 来源:网络整理
导读:我有两个与他们的名字完全相同的匹配器: RSpec::Matchers.define :be_the_downcased_version_of do |actual| match do |actual| @actual = actual @expected = expected @actual == @expected.downcase end failure_message do |actual| "Expected #{@actua
我有两个与他们的名字完全相同的匹配器:
RSpec::Matchers.define :be_the_downcased_version_of do |actual| match do |actual| @actual = actual @expected = expected @actual == @expected.downcase end failure_message do |actual| "Expected #{@actual.inspect} to be the downcased version of #{expected.inspect}.nIt was not: #{@actual.inspect}" end failure_message_when_negated do |actual| "Expected #{@actual.inspect} to not be the downcased version of #{expected.inspect}.nIt was: #{@actual.inspect}" end description do "be the downcased version of #{expected.inspect}" end end RSpec::Matchers.define :return_the_downcased_version_of do |actual| match do |actual| @actual = actual @expected = expected @actual == @expected.downcase end failure_message do |actual| "Expected #{@actual.inspect} to be the downcased version of #{expected.inspect}.nIt was not: #{@actual.inspect}" end failure_message_when_negated do |actual| "Expected #{@actual.inspect} to not be the downcased version of #{expected.inspect}.nIt was: #{@actual.inspect}" end description do "be the downcased version of #{expected.inspect}" end end 我可以像使用Ruby别名方法一样对它们进行别名吗? alias_method :foo_meth,:bar_meth 解决方法
是的,您可以为使用RSpec的匹配器DSL定义的匹配器添加别名.我知道这样做最方便的方法是在before块中重新打开示例上下文的类,并在那里为方法添加别名.
但首先,让我们清理你的匹配器. >传递给Matchers.define的块采用预期值,而不是 在spec / support / be_the_downcased_version_of.rb中: RSpec::Matchers.define :be_the_downcased_version_of do |expected| match do |actual| actual == expected.downcase end failure_message_for_should do |actual| "Expected #{actual.inspect} to be the downcased version of #{expected.inspect}.nIt was not: #{actual.inspect}" end failure_message_for_should_not do |actual| "Expected #{actual.inspect} to not be the downcased version of #{expected.inspect}.nIt was: #{actual.inspect}" end description do "be the downcased version of #{expected.inspect}" end end 要在spec / spec_helper.rb中为该匹配器定义的方法添加别名: config.before :each do class << self alias_method :return_the_downcased_version_of,:be_the_downcased_version_of end end 这是另一种方法.我实际上不会这样做,因为它需要更多的样板,但它比以前的方法使用更少的魔力,因此它可能提供信息.只需在没有DSL的情况下实现匹配器并正常使用该方法.在spec / support / be_the_downcased_version_of.rb中: alias_method :return_the_downcased_version_of,:be_the_downcased_version_of def be_the_downcased_version_of(actual) BeTheDowncasedVersionOf.new actual end class BeTheDowncasedVersionOf def initialize(expected) @expected = expected end def matches?(actual) @actual = actual @actual == @expected.downcase end def failure_message_for_should "Expected #{@actual.inspect} to be the downcased version of #{@expected.inspect}.nIt was not: #{@actual.inspect}" end def failure_message_for_should_not "Expected #{@actual.inspect} to not be the downcased version of #{@expected.inspect}.nIt was: #{@actual.inspect}" end def description "be the downcased version of #{@expected.inspect}" end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |