Class: Minitest::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/matchers.rb

Class Method Summary collapse

Class Method Details

.register_matcher(matcher, name, exp_name = name) ⇒ Object

Defines assert_* assertion and must_* expectation for a matcher

Example:

# Say you're implementing Capybara's HaveContent matcher

class HaveContent
  # ...
end

Minitest::Test.register_matcher HaveContent, :have_content

class MyTest < Minitest::Test
  def test_index
    visit "/"
    assert_have_content page, "Hello"
    assert_have_selector page, :xpath, "//table/tr"
  end
end

describe "my test" do
  test "index" do
    visit "/"
    page.must_have_content "Hello"
    page.must_have_selector :xpath, "//table/tr"

    # if you set `page` to be implicit subject, following works too:
    must_have_content "Hello"
  end
end


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/minitest/matchers.rb', line 169

def self.register_matcher matcher, name, exp_name=name
  define_method :"assert_#{name}" do |*args|
    subject = args.shift

    x = if Symbol === matcher
          send matcher, *args
        else
          matcher.new(*args)
        end

    assert_must x, subject
  end

  Object.infect_an_assertion :"assert_#{name}", :"must_#{exp_name}", true

  define_method :"refute_#{name}" do |*args|
    subject = args.shift

    x = if Symbol === matcher
          send matcher, *args
        else
          matcher.new(*args)
        end

    assert_wont x, subject
  end

  Object.infect_an_assertion :"refute_#{name}", :"wont_#{exp_name}", true
end