Skip to content

kachick/rspec-matchers-power_assert_matchers

Repository files navigation

rspec-matchers-power_assert_matchers

Build Status Gem Version

Overview

screenshot1 screenshot2

ruby/power_assert integration for RSpec.

No monkey patching exists.
Just using RSpec's Custom matchers way.
I hope this might be a choice to integrate PowerAssert in RSpec.

Usage

Require following dependencies

  • Ruby 3.2 or higher # Tested only in the head and last 2 rubies
  • rspec
  • power_assert
  • irb # To colorize

See gemspec for detail of the version restriction.

Add below code into your library's gemspec

Gem::Specification.new do |gem|
  gem.add_development_dependency 'rspec-matchers-power_assert_matchers', '~> 0.2.0'
end

Or add below into your application's Gemfile

group :test do
  gem 'rspec-matchers-power_assert_matchers', '~> 0.2.0'
end

So adding this into your spec_helper.rb or rails_helper.rb

require 'rspec/matchers/power_assert_matchers'

Then you can use power_assert with a simple custom matcher in rspec.

Use expect with block, and use be_assert matcher.

RSpec.describe RSpec::Matchers::PowerAssertMatchers do
  context 'when the given block returns falsy' do
    it 'shows power_assert inspection' do
      expect {
        '0'.class == '3'.to_i.times.map {|i| i + 1 }.class
      }.to be_assert
    end
  end
end
  1) RSpec::Matchers::PowerAssertMatchers when the given block returns falsy shows power_assert inspection
     Failure/Error:
       expect {
         '0'.class == '3'.to_i.times.map {|i| i + 1 }.class
       }.to be_assert

               '0'.class == '3'.to_i.times.map {|i| i + 1 }.class
                   |     |      |    |     |                |
                   |     |      |    |     |                Array
                   |     |      |    |     [1, 2, 3]
                   |     |      |    #<Enumerator: ...>
                   |     |      3
                   |     false
                   String
    # ./examples/spec/example_spec.rb:6:in `block (3 levels) in <top (required)>'

If you want to easy to check the behavior, this repository has example specs.

$ git clone [email protected]:kachick/rspec-matchers-power_assert_matchers.git
$ cd rspec-matchers-power_assert_matchers
$ bundle install
$ bundle exec rake look_at_examples
Rough examples will be shown!
$ bundle exec rake run_examples
Some results will be shown!

NOTE

  • Currently the matcher is named be_assert. I don't have confident it is good or not.
  • Extending built-in be_truthy sounds good for considering the role, but I would not like to add monkey patching for built-in features...

References