How to use RSpec without Rails?
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.rbclass MyModel def the_truth true endend#spec/my_model_spec.rbrequire_relative '../my_model'describe MyModel do it "should be true" do MyModel.new.the_truth.should be_true endend
Then in the console run
rspec spec/my_model_spec.rb
voila!
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 bundlerbundle installbundle 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 endend
And run the specs:
% bundle exec rspec.Finished in 0.00325 seconds (files took 0.09777 seconds to load)1 example, 0 failures