RSpec is a testing framework for Ruby. This posts show how to create a rake task that executes rspec tests. RSpec tests will be running using command ‘rake spec’.
Rake task in Rails application
If you have Rails application you can use gem ‘rspec-rails‘.
After adding to Gemfile and installing it using command
rails generate rspec:install
our project structure will be like this:
Our tests will be located in folders inside spec/ folder. For examples, spec/features, spec/models, etc.
To run all tests use
or run tests in a specific file:
rspec spec SPEC=spec/models/mymodel_spec.rb
Rake task without Rails
It creates files:
Create your tests in spec/ folder.
Rake directives can be located either in “rakefile”, “Rakefile”, “rakefile.rb” or “Rakefile.rb” file.
require 'rake' require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = Dir.glob('spec/**/*_spec.rb') t.rspec_opts = '--format documentation' # t.rspec_opts << ' more options' t.rcov = true end task :default => :spec
Now we can run all tests located in spec/ folder:
Run tests in a specific file:
rake spec SPEC=spec/features/myfeature_spec.rb
rake tasks for RSpec: