10

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

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具体记录为:

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

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

4

3 回答 3

8

RSpec::Matchers似乎是受支持的 API(它的 rdoc 没有另外说明),因此您可以在 Ruby 中而不是在 matcher DSL 中编写匹配器(这是受支持的;请参阅自定义匹配器文档的第二段)并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
于 2016-08-17T12:43:00.810 回答
1

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

我有一个非常复杂的控制器,带有一个选项卡式界面,其中定义和选择的选项卡取决于模型实例的状态。我需要在 :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 编写匹配器,就能将多个重复的期望块合并成一组自定义匹配器,这真是太棒了。

这为我节省了数十行代码,使我的测试更具表现力,并允许我在填充这些选项卡的逻辑发生变化时在一个地方进行更改。

另请注意,您可以在自定义匹配器中访问方法/变量,例如assignsand ,controller因此您不需要显式传递它们。

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

于 2018-04-20T23:48:53.227 回答
0

您可以使用 matcher DSL 而不是编写自己的Matcher类。这有点简单。

RSpec::Matchers.define :have_query_params do |expected|
  match do |actual|
    # your code
    RSpec::Matchers::BuiltIn::Include.new(expected).matches?(actual)
  end
end
于 2021-11-30T20:03:54.047 回答