module RSpec::XUnit::Assertions
Assertions
contains the XUnit
friendly assertions to be used in RSpec
examples.
Constants
- ASSERTION_NEGATIVE_PREDICATE_REGEX
- ASSERTION_NEGATIVE_REGEX
- ASSERTION_PREDICATE_REGEX
- ASSERTION_REGEX
Public Class Methods
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`
# 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
Public Instance Methods
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 }`
# File lib/rspec/xunit/assertions.rb, line 72 def assert!(value = Expectations::ExpectationTarget::UndefinedValue, &block) Expectations::ExpectationTarget.for(value, block) end
Mock is an XUnit
alternative to the `expect` based mocking syntax.
`mock(Post).to receive(:comments)`
# 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 is an XUnit
alternative to the `expect_any_instance_of` based mocking syntax.
`mock_any_instance_of(Post).to receive(:comments)`
# File lib/rspec/xunit/assertions.rb, line 89 def mock_any_instance_of(klass) RSpec::Mocks::AnyInstanceExpectationTarget.new(klass) end
Stub is an XUnit
alternative to the `allow` based mocking syntax.
# File lib/rspec/xunit/assertions.rb, line 94 def stub(target) RSpec::Mocks::AllowanceTarget.new(target) end
Stub any instance of is an XUnit
alternative to the `allow` based mocking syntax.
# File lib/rspec/xunit/assertions.rb, line 100 def stub_any_instance_of(klass) RSpec::Mocks::AnyInstanceAllowanceTarget.new(klass) end
Private Instance Methods
TODO(genadi): Figure out where exactly the code shown in the failure message is extracted.
# File lib/rspec/xunit/assertions.rb, line 155 def adjust_for_better_failure_message(backtrace) backtrace.drop_while { |trace| !trace.include?(__FILE__) }[1..-1] end
# 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
# File lib/rspec/xunit/assertions.rb, line 149 def respond_to_missing?(method, *) method =~ ASSERTION_PREDICATE_REGEX || super end