How to use RSpec without Rails?
Solution 1:
The process is as follows:
Install the rspec gem from the console:
gem install rspec
Then create a folder (we'll name it root) with the following content:
root/my_model.rb
root/spec/my_model_spec.rb
#my_model.rb
class MyModel
def the_truth
true
end
end
#spec/my_model_spec.rb
require_relative '../my_model'
describe MyModel do
it "should be true" do
MyModel.new.the_truth.should be_true
end
end
Then in the console run
rspec spec/my_model_spec.rb
voila!
Solution 2:
From within your projects directory...
gem install rspec
rspec --init
then write specs in the spec dir created and run them via
rspec 'path to spec' # or just rspec to run them all
Solution 3:
The workflows around gem install rspec
are flawed. Always use Bundler and Gemfile to ensure consistency and avoid situations where a project works correctly on one computer but fails on another.
Create your Gemfile
:
source 'https://rubygems.org/'
gem 'rspec'
Then execute:
gem install bundler
bundle install
bundle exec rspec --init
The above will create .rspec
and spec/spec_helpers.rb
for you.
Now create your example spec in spec/example_spec.rb
:
describe 'ExampleSpec' do
it 'is true' do
expect(true).to be true
end
end
And run the specs:
% bundle exec rspec
.
Finished in 0.00325 seconds (files took 0.09777 seconds to load)
1 example, 0 failures