Module: RSpec::XUnit::Assertions

Defined in:
lib/rspec/xunit/assertions.rb

Overview

Assertions contains the XUnit friendly assertions to be used in RSpec examples.

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rspec/xunit/assertions.rb', line 111

def method_missing(method, *args, &block)
  return if ASSERTION_NEGATIVE_PREDICATE_REGEX.match(method.to_s) do |match|
    value = args.shift
    matcher = "be_#{match[1]}"

    expect(value).to_not Matchers::BuiltIn::BePredicate.new(matcher, *args, &block)
  end

  return if ASSERTION_PREDICATE_REGEX.match(method.to_s) do |match|
    value = args.shift
    matcher = "be_#{match[1]}"

    expect(value).to Matchers::BuiltIn::BePredicate.new(matcher, *args, &block)
  end

  return if ASSERTION_NEGATIVE_REGEX.match(method.to_s) do |match|
    matcher = match[1]

    RSpec::XUnit::Assertions.module_eval do
      assertion_match matcher, block: block
    end

    send "assert_not_#{matcher}", *args, &block
  end

  return if ASSERTION_REGEX.match(method.to_s) do |match|
    matcher = match[1]

    RSpec::XUnit::Assertions.module_eval do
      assertion_match matcher, block: block
    end

    send "assert_#{matcher}", *args, &block
  end

  super
end

Class Method Details

.assertion_match(matcher, suffix = matcher, block: false) ⇒ Object

Assertion match converts RSpec matchers into an XUnit friendly assertions.

For example, ‘assertion_match :eq` will create two methods:

  • ‘assert_eq` roughly `expect(action).to eq(expected)`

  • ‘assert_not_eq` roughly `expect(action).to_not eq(expected)`

For block expectation, ‘assertion_match :raises, :raise_error, block: true` will generate two methods:

  • ‘assert_raises` roughly `expect { bloc }.to raise_error`

  • ‘assert_not_raises` roughly `expect { bloc }.to_not raise_error`



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rspec/xunit/assertions.rb', line 22

def assertion_match(matcher, suffix = matcher, block: false)
  if block
    define_method "assert_#{suffix}" do |*args, &block|
      expect(&block).to send(matcher, *args)
    rescue Expectations::ExpectationNotMetError => e
      raise e, e.message, adjust_for_better_failure_message(e.backtrace), cause: nil
    end

    define_method "assert_not_#{suffix}" do |*args, &block|
      expect(&block).to_not send(matcher, *args)
    rescue Expectations::ExpectationNotMetError => e
      raise e, e.message, adjust_for_better_failure_message(e.backtrace), cause: nil
    end
  else
    define_method "assert_#{suffix}" do |value, *args, &blk|
      expect(value).to send(matcher, *args, &blk)
    rescue Expectations::ExpectationNotMetError => e
      raise e, e.message, adjust_for_better_failure_message(e.backtrace), cause: nil
    end

    define_method "assert_not_#{suffix}" do |value, *args, &blk|
      expect(value).to_not send(matcher, *args, &blk)
    rescue Expectations::ExpectationNotMetError => e
      raise e, e.message, adjust_for_better_failure_message(e.backtrace), cause: nil
    end
  end
end

Instance Method Details

#assert!(value = Expectations::ExpectationTarget::UndefinedValue, &block) ⇒ Object Also known as: assert

Assert is an alias to ‘expect`. Use it when all else fails or doesn’t feel right. The ‘change` assertion with a block is a good example:

‘assert! { block }.to change { value }` or `assert { block }.to change { value }`



72
73
74
# File 'lib/rspec/xunit/assertions.rb', line 72

def assert!(value = Expectations::ExpectationTarget::UndefinedValue, &block)
  Expectations::ExpectationTarget.for(value, block)
end

#mock(value = Expectations::ExpectationTarget::UndefinedValue, &block) ⇒ Object

Mock is an XUnit alternative to the ‘expect` based mocking syntax.

‘mock(Post).to receive(:comments)`



81
82
83
# File 'lib/rspec/xunit/assertions.rb', line 81

def mock(value = Expectations::ExpectationTarget::UndefinedValue, &block)
  Expectations::ExpectationTarget.for(value, block)
end

#mock_any_instance_of(klass) ⇒ Object

Mock any instance of is an XUnit alternative to the ‘expect_any_instance_of` based mocking syntax.

‘mock_any_instance_of(Post).to receive(:comments)`



89
90
91
# File 'lib/rspec/xunit/assertions.rb', line 89

def mock_any_instance_of(klass)
  RSpec::Mocks::AnyInstanceExpectationTarget.new(klass)
end

#stub(target) ⇒ Object

Stub is an XUnit alternative to the ‘allow` based mocking syntax.



94
95
96
# File 'lib/rspec/xunit/assertions.rb', line 94

def stub(target)
  RSpec::Mocks::AllowanceTarget.new(target)
end

#stub_any_instance_of(klass) ⇒ Object

Stub any instance of is an XUnit alternative to the ‘allow` based mocking syntax.



100
101
102
# File 'lib/rspec/xunit/assertions.rb', line 100

def stub_any_instance_of(klass)
  RSpec::Mocks::AnyInstanceAllowanceTarget.new(klass)
end