New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

minitest-should_just_work

Package Overview
Dependencies
Maintainers
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minitest-should_just_work

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
2
Created
Source

Minitest::ShouldJustWork

Classic Rspec-like .should/.should_not matching for MiniTest.

This gem is heavely inspired from the minitest-should_syntax gem, what has not updated a long time. Therefore we need this new gem to support .should syntax.

minitest-should_just_work lets you use a syntax similar to classic RSpec on your MiniTest tests. It monkey-patches Object to translate RSpec-like sugar into plain MiniTest assert matchers.

(Ouch! Monkey patch? Yes! But it only defines Object#should and #should_not.)

Installation

Install the gem and add to the application's Gemfile by executing:

group :test do
  gem 'minitest-spec-rails'
  gem 'minitest-should_just_work'
end
$ bundle

Usage

require 'minitest/autorun'
require 'minitest/should_just_work'

describe "Books" do
  it "should work" do
    book = Book.new title: "Revolution"

    book.title.should == "Revolution"
  end
end

Then you may use it as so:

  obj.should == 2                    # => assert_equal 2, obj
  obj.should =~ /regex/              # => assert_match /regex/, obj
  obj.should != 3                    # => assert_not_equal 3, obj
  obj.should.nil                     # => assert_nil obj
  obj.should.respond_to(:freeze)     # => assert_respond_to obj, :freeze 

# Note that .be, .a and .an are optional.
  obj.should.nil                     # => assert_nil obj
  obj.should.be.nil                  # => assert_nil obj
  obj.should.be.a.nil                # => assert_nil obj

# Use `should.not` instead `should` to negate any comparison.
  obj.should.not == 3                # => refute_equal 3, obj
  obj.should.not =~ /regex/          # => refute_match obj, regex
  obj.should.not.be.nil              # => refute obj.nil?

# Anything else will pass through with a ?:
  obj.should.be.good_looking         # => assert obj.good_looking?

# Testing exceptions:
  should.raise ZeroDivisionError do
    2/0
  end

Wrapped assertions

These are based on MiniTest::Assertions.

MiniTest::ShouldJustWorkMiniTest::Assertions
x.should.equal yassert_equal x, y
x.should == yassert_equal x, y
x.should.not.equalrefute_equal x, y
x.should !=refute_equal x, y
x.should.beassert_same x, y
x.should.not.berefute_same x, y
x.should >= (and others)assert_operator x, :>=, y
x.should.not >= (and others)refute_operator x, :>=, y
x.should.be.nilassert_nil x
x.should.not.be.nilrefute_nil x
x.should.be.close yassert_in_delta y
x.should.be.in_epsilon yassert_in_epsilon y
x.should.match /y/assert_match x, /y/
x.should =~ /y/assert_match x, /y/
x.should.not.match, should.not =~refute_match
x.should.be.an.instance_of yassert_instance_of x, y
x.should.be.a.kind_of x, yassert_kind_of x, y
x.should.respond_to :yassert_respond_to x, :y
should.raise(x) { ... }assert_raise(x) { ... }
should.throw(x) { ... }assert_throws(x) { ... }
should.satisfy { ... }assert_block { ... }

Messages

Use the otherwise helper:

it "should work" do
  book = Book.new title: "Pride & Prejudice"

  otherwise "The title should've been set on constructor"
  book.title.should == "Pride & Prejudice"
end

Result:

1) Failure:
should work(Test) [your_test.rb:77]:
The title should've been set on constructor.
Expected: "Pride & Prejudice"
  Actual: nil

Or you can use .blaming which does the same thing (with a more cumbersome syntax):

it "should work" do
  book = Book.new title: "Pride & Prejudice"

  message = "The title should've been set on constructor"
  book.title.should.blaming(message) == "Pride & Prejudice"
end

Extending

Need to create your own matchers? Create your new matcher in a module, then use MiniTest::ShouldJustWork.add.

module DanceMatcher
  def boogie_all_night!
    # Delegates to `assert(condition, message)`.
    #
    #   positive?   - returns `true` if .should, or `false` if .should.not
    #   test        - the MiniTest object
    #   msg         - the failure message. `nil` if not set
    #
    if positive?
      test.assert left.respond_to?(:dance), msg
    else
      test.refute left.respond_to?(:dance), msg
    end
  end
end

MiniTest::ShouldJustWork.add DanceMatcher

# Then in your tests, use:
dancer.should.boogie_all_night!

Acknowledgements & licensing

(c) 2022 Priit Tark, MIT license (c) 2013 Rico Sta. Cruz, MIT license

FAQs

Package last updated on 21 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc