我可以在自定义匹配器中使用内置RSpec匹配器吗?

时间:2016-08-17 08:40:50

标签: ruby rspec rspec-expectations

我在功能规范中有以下期望(非常低级但仍然必要):

expect(Addressable::URI.parse(current_url).query_values).to include(
  'some => 'value',
  'some_other' => String
)

请注意,第二个查询值是模糊匹配,因为我只是想确保它在那里,但我不能更具体地说明它。

我想将其提取到自定义匹配器中。我开始时:

RSpec::Matchers.define :have_query_params do |expected_params|
  match do |url|
    Addressable::URI.parse(url).query_values == expected_params
  end
end

但这意味着我无法通过{'some_other' => String}。要继续使用模糊匹配,我必须在自定义匹配器中使用include匹配器。

但是,RSpec::Matchers::BuiltIn中的任何内容都标记为私有API,Include具体为documented

# Provides the implementation for `include`.
# Not intended to be instantiated directly.

所以,我的问题是:在RSpec支持的自定义匹配器中使用内置匹配器吗?我该怎么做?

2 个答案:

答案 0 :(得分:4)

RSpec::Matchers似乎是受支持的API(其rdoc没有说明),因此您可以在Ruby中编写匹配器,而不是在matcher DSL中编写(支持;请参阅{{3} })并像这样使用RSpec::Matchers#include

规格/支持/ matchers.rb

module My
  module Matchers
    def have_query_params(expected)
      HasQueryParams.new expected
    end

    class HasQueryParams
      include RSpec::Matchers

      def initialize(expected)
        @expected = expected
      end

      def matches?(url)
        actual = Addressable::URI.parse(url).query_values
        @matcher = include @expected
        @matcher.matches? actual
      end

      def failure_message
        @matcher.failure_message
      end

    end
  end
end

规格/支持/ matcher_spec.rb

include My::Matchers

describe My::Matchers::HasQueryParams do
  it "matches" do
    expect("http://example.com?a=1&b=2").to have_query_params('a' => '1', 'b' => '2')
  end
end

答案 1 :(得分:0)

是的,您可以在自定义匹配器中调用内置的rspec匹配器。换句话说,在编写匹配器时,可以使用普通的Rspec DSL而不是纯Ruby。查看this gist(不是我的要点,但它帮助了我!)。

我有一个非常复杂的控制器,带有选项卡式界面,其中定义和选择的选项卡取决于模型实例的状态。我需要在:new,:create,:edit和:update操作的每个状态下测试选项卡设置。所以我写了这些匹配器:

require "rspec/expectations"

RSpec::Matchers.define :define_the_review_tabs do
  match do
    expect(assigns(:roles         )).to be_a_kind_of(Array)
    expect(assigns(:creators      )).to be_a_kind_of(ActiveRecord::Relation)
    expect(assigns(:works         )).to be_a_kind_of(Array)

    expect(assigns(:available_tabs)).to include("post-new-work")
    expect(assigns(:available_tabs)).to include("post-choose-work")
  end

  match_when_negated do
    expect(assigns(:roles         )).to_not be_a_kind_of(Array)
    expect(assigns(:creators      )).to_not be_a_kind_of(ActiveRecord::Relation)
    expect(assigns(:works         )).to_not be_a_kind_of(Array)

    expect(assigns(:available_tabs)).to_not include("post-new-work")
    expect(assigns(:available_tabs)).to_not include("post-choose-work")
  end

  failure_message do
    "expected to set up the review tabs, but did not"
  end

  failure_message_when_negated do
    "expected not to set up review tabs, but they did"
  end
end

RSpec::Matchers.define :define_the_standalone_tab do
  match do
    expect(assigns(:available_tabs)).to include("post-standalone")
  end

  match_when_negated do
    expect(assigns(:available_tabs)).to_not include("post-standalone")
  end

  failure_message do
    "expected to set up the standalone tab, but did not"
  end

  failure_message_when_negated do
    "expected not to set up standalone tab, but they did"
  end
end

RSpec::Matchers.define :define_only_the_review_tabs do
  match do
    expect(assigns).to     define_the_review_tabs
    expect(assigns).to_not define_the_standalone_tab
    expect(assigns(:selected_tab)).to eq(@selected) if @selected
  end

  chain :and_select do |selected|
    @selected = selected
  end

  failure_message do
    if @selected
      "expected to set up only the review tabs and select #{@selected}, but did not"
    else
      "expected to set up only the review tabs, but did not"
    end
  end
end

RSpec::Matchers.define :define_only_the_standalone_tab do
  match do
    expect(assigns).to     define_the_standalone_tab
    expect(assigns).to_not define_the_review_tabs
    expect(assigns(:selected_tab)).to eq("post-standalone")
  end

  failure_message do
    "expected to set up only the standalone tab, but did not"
  end
end

RSpec::Matchers.define :define_all_tabs do
  match do
    expect(assigns).to define_the_review_tabs
    expect(assigns).to define_the_standalone_tab
    expect(assigns(:selected_tab)).to eq(@selected) if @selected
  end

  chain :and_select do |selected|
    @selected = selected
  end

  failure_message do
    if @selected
      "expected to set up all three tabs and select #{@selected}, but did not"
    else
      "expected to set up all three tabs, but did not"
    end
  end
end

我正在使用它们:

should define_all_tabs.and_select("post-choose-work")
should define_all_tabs.and_select("post-standalone")
should define_only_the_standalone_tab
should define_only_the_review_tabs.and_select("post-choose-work")
should define_only_the_review_tabs.and_select("post-new-work")

超级棒,能够只需要几个重复的期望,并将它们组合成一组自定义匹配器,而无需在纯Ruby中编写匹配器。

这为我节省了数十行代码,使我的测试更具表现力,并且如果填充这些标签的逻辑发生变化,我可以在一个地方更改内容。

另请注意,您可以在自定义匹配器中访问方法/变量,例如assignscontroller,因此您无需明确传递它们。

最后,我可以在规范中内联定义这些匹配器,但我选择将它们放在spec / support / matchers / controllers / posts_controller_matchers.rb