Module: CapybaraMiniTestSpec

Defined in:
lib/capybara_minitest_spec.rb,
lib/capybara_minitest_spec/version.rb,
lib/capybara_minitest_spec/test_name.rb

Defined Under Namespace

Classes: NegativeTestName, PositiveTestName, TestName

Constant Summary collapse

VERSION =
'1.0.3'

Class Method Summary collapse

Class Method Details

.add_matcher(matcher_name) ⇒ Object

Define assertions and spec expectations for both positive and negative forms of the matcher name.



14
15
16
17
18
19
20
# File 'lib/capybara_minitest_spec.rb', line 14

def add_matcher(matcher_name)
  positive_name = CapybaraMiniTestSpec::PositiveTestName.new(matcher_name)
  negative_name = CapybaraMiniTestSpec::NegativeTestName.new(matcher_name)
  [positive_name, negative_name].each do |test_name|
    CapybaraMiniTestSpec.define_expectation(test_name)
  end
end

.define_assertion(test_name) ⇒ Object

Define an assertion using test_name. For example, if the test name is have_css, the assertion would be called assert_page_has_css.

Use either of Capybara::RSpecMatchers. Assert that it matches, and pass its failure message if it doesn’t.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/capybara_minitest_spec.rb', line 33

def define_assertion(test_name)
  method_name = test_name.assertion_name
  MiniTest::Assertions.send :define_method, method_name do |page, *args|
    matcher = test_name.matcher(*args)

    matches = matcher.send(test_name.match_method, page)
    failure_message = message do
      matcher.send(test_name.failure_message_method)
    end

    assert matches, failure_message
  end
end

.define_expectation(test_name) ⇒ Object



22
23
24
25
# File 'lib/capybara_minitest_spec.rb', line 22

def define_expectation(test_name)
  define_assertion(test_name)
  infect_assertion(test_name)
end

.infect_assertion(test_name) ⇒ Object

Define the MiniTest::Spec expectation.



48
49
50
# File 'lib/capybara_minitest_spec.rb', line 48

def infect_assertion(test_name)
  MiniTest::Expectations.infect_an_assertion test_name.assertion_name, test_name.expectation_name, true
end