ruby – 我可以为RSpec匹配器添加别名吗?

ruby – 我可以为RSpec匹配器添加别名吗?,第1张

概述我有两个与他们的名字完全相同的匹配器: RSpec::Matchers.define :be_the_downcased_version_of do |actual| match do |actual| @actual = actual @expected = expected @actual == @expected.downcase 我有两个与他们的名字完全相同的匹配器:

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}"    endendRSpec::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}"    endend

我可以像使用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}"  endend

要在spec / spec_helper.rb中为该匹配器定义的方法添加别名:

config.before :each do  class << self    alias_method :return_the_downcased_version_of,:be_the_downcased_version_of  endend

这是另一种方法.我实际上不会这样做,因为它需要更多的样板,但它比以前的方法使用更少的魔力,因此它可能提供信息.只需在没有DSL的情况下实现匹配器并正常使用该方法.在spec / support / be_the_downcased_version_of.rb中:

alias_method :return_the_downcased_version_of,:be_the_downcased_version_ofdef be_the_downcased_version_of(actual)  BeTheDowncasedVersionOf.new actualendclass 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}"  endend
总结

以上是内存溢出为你收集整理的ruby – 我可以为RSpec匹配器添加别名吗?全部内容,希望文章能够帮你解决ruby – 我可以为RSpec匹配器添加别名吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1276161.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存