https://github.com/michaeljklein/nil-passer
Pass nils to single-arg blocks in Ruby on Rails for testing
https://github.com/michaeljklein/nil-passer
nil-passer rails ruby testing-tools
Last synced: 2 months ago
JSON representation
Pass nils to single-arg blocks in Ruby on Rails for testing
- Host: GitHub
- URL: https://github.com/michaeljklein/nil-passer
- Owner: michaeljklein
- License: mit
- Created: 2017-03-08T01:11:19.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-11T18:04:27.000Z (over 8 years ago)
- Last Synced: 2025-03-06T18:50:34.205Z (over 1 year ago)
- Topics: nil-passer, rails, ruby, testing-tools
- Language: Ruby
- Homepage: https://michaeljklein.github.io/nil-passer/
- Size: 86.9 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nil-Passer
## Status
[](https://travis-ci.org/michaeljklein/nil-passer)
[](https://codeclimate.com/github/michaeljklein/nil-passer/maintainability)
[](https://codeclimate.com/github/michaeljklein/nil-passer/test_coverage)
[](https://badge.fury.io/rb/nil-passer)
[](https://www.codacy.com/app/michaeljklein/nil-passer?utm_source=github.com&utm_medium=referral&utm_content=michaeljklein/nil-passer&utm_campaign=Badge_Grade)
[](https://gitter.im/nil-passer/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
## Introduction
Ever been sick of blocks not handling nil in Ruby, or more specifically Rails?
This gem defines the [`NilPasser`](https://github.com/michaeljklein/nil-passer/blob/master/lib/nil_passer.rb#L3) class, which can be used to monitor all passing of blocks on an object, through a specific method, or to a class.
Each block passed is passed `nil` and the execution _usually_ continues normally. (Usually meaning you probably don't want to use this in production.)
During or after execution, you can `grep '^[no_nil]'` on your logs to get all the results.
Loosely inspired by the [`bullet`](https://github.com/flyerhzm/bullet) gem.
# Examples
From [the tests](https://github.com/michaeljklein/nil-passer/blob/master/test/test_nil_passer.rb), we have example usage for "good, bad, and subtle" blocks:
## Good
This is the control: we provide the caller's location and the identity (function) block ([source](https://github.com/michaeljklein/nil-passer/blob/master/test/test_nil_passer.rb#L45)):
```
def test_test_ignores_good_block
assert @log.blank?
NilPasser.test [Rails.path], Proc.new{|x| x}
assert @log.blank?
end
```
## Bad
This is a simple exception-handling case, where the block accepts `nil` but always raises an exception ([source](https://github.com/michaeljklein/nil-passer/blob/master/test/test_nil_passer.rb#L51)):
```
def test_test_catches_bad_block
assert @log.blank?
NilPasser.test [Rails.path], Proc.new{|x| (raise "hi")}
assert !@log.blank?
end
```
## Subtle
This is an example of a block that _only_ raises an exception when passed `nil` ([source](https://github.com/michaeljklein/nil-passer/blob/master/test/test_nil_passer.rb#L57)):
```
def test_test_catches_subtle_block
assert @log.blank?
NilPasser.test [Rails.path], Proc.new{|x| x.nil? && (raise "hi")}
assert !@log.blank?
end
```