schrodingersbox/spec_cat
This gem contains trivial matchers to make RSpecs a bit more effective and less annoying.
- eql_file
- have_a_spec
- include_module
- pass_rubocop
- validates_with
It also provides rake commands
- rake spec_cat:accept
- spec_cat:coverage
NOTE: This gem does not depend on Rails. All paths are relative to cwd, which may be Rails.root or anywhere else.
Getting Started
Add this to your gemfile...
gem 'spec_cat'
gem 'simplecov', :require => false
Add this to your spec_helper.rb...
# Initialize SimpleCov
require 'simplecov'
SimpleCov.start 'rails' do
add_filter '/vendor/'
add_filter '/spec/'
end
Matchers
eql_file
eql_file
will compare method output to a ground truth file and fail if they
are different.
It also writes a .tmp file to replace the old ground truth if it's gone stale.
e.g. #foo produces a gnarly string too nasty to copy and paste into spec code.
expect(foo).to eql_file('spec/truth/foo.json')
... if it fails for a valid change, you can just....
cp spec/truth/foo.json.tmp spec/truth/foo.json
... and all will be good again.
This mechanism is a bit brittle, but great for big blobs of data.
If you use this, you should add *.tmp
to your .gitignore.
have_a_spec
have_a_spec
will ensure that any given path has a corresponding spec file to
help ensure that you have good coverage.
expect('app/controllers/application_controller.rb').to have_a_spec
... is a good thing to write right after you integrate RSpec.
Here's an example coverage spec...
https://github.com/schrodingersbox/spec_cat/blob/master/spec/coverage_spec.rb
include_module
include_module
makes it easy to spec concern inclusion.
it('is taggable') { is_expected.to include_module(Taggable) }
pass_rubocop
pass_rubcop
just executes Rubocop and passes or fails
based on it's exit status.
it('passes Rubocop') { is_expected.to pass_rubocop }
validates_with
validates_with
confirms that an ActiveModel::Validator
is being used.
it('validates numbers') { is_expected.to validate_with(NumberValidator) }
Rake Tasks
spec_cat:accept
rake spec_cat:accept
runs all specs and causes the eql_file matcher to overwrite
the ground truth files, rather than output .tmp files.
This is convenient when a code change impacts a large number of ground truth files, but is also risky, as it may allow an incorrect change to become ground truth.
spec_cat:coverage
rake spec_cat:coverage
runs all specs and then opens the coverage report if all the
specs pass.
Reference
- RSpec
- Rubocop
- Better Specs
- Publishing your gem
- Testing Rake Tasks with RSpec
- Nathan Humbert's Blog: Rails 3: Loading rake tasks from a gem
- Add Achievement Badges to Your Gem README
Version History
- 3.1.3 - Adding
validate_with
matcher from https://gist.github.com/Bartuz/98abc9301adbc883b510 - 3.1.0 - Rubocop integration
- 3.0.0 - RSpec 3 supported
- 1.0.3 - Last version with RSpec 2.x support
Credits
Thanks to Filip Bartuzi and Otavio Medeiros
for publishing their gists of the validate_with
matcher!