RSpec cheatsheet
RSpec cheatsheet
install rspec-rails gem only, it will install all the rspec related gems.Basic setup steps
Step1:
gem 'rspec-rails'
root@f79191f58a4f:~# gem list rspec
*** LOCAL GEMS ***
rspec-core (3.9.3)
rspec-expectations (3.9.4)
rspec-mocks (3.9.1)
rspec-rails (3.9.1)
rspec-support (3.9.4)
Step2:
rails generate rspec:install
it will create 3 files,
.rspec
spec/rails_helper.rb
spec/spec_helper.rb
Step3:
put these two lines in the .rspec file,
--require spec_helper
--format documentation
Step4:
run rspec as,
rspec
------
Basic knowledge
All the spec(specification) files stay inside spec folder.
Allthe spec file ends with *_spec.rb
Example - spec/sales_spec.rb
All files in this directory(spec/**/*_spec.rb)will be run automatically when we run,
rspec
Dry run spec as,
rspec --dry-run
Run a particular spec,
rspec spec/sales_spec.rb
Run a particular spec from a particular line,
rspec spec/coffee_spec.rb:31
Run rspec with formatted output as,
rspec --format doc
'describe' is used to group 'it'
RSpec.describe 'An good sales' do
it 'is not fare deal' do
expect(sale_price).to eq(cost_price)
end
end
This is called Assertion,
expect(sale_price).to eq(cost_price)
To share common code we use before, let, helper method.
let execute only ones.
helper_method execute on each call.
before execute just before every 'it'
context is used to group similar test.
describe -> before, let, helper method, it, context -> before,it
RSpec.describe 'A coffee' do
let(:user_name) { User.new('Aarav Sharma') }
it 'has name' do
expect(user_name).to eq('Aarav Sharma')
end
context 'User info' do
before { user_name.upcase }
it 'has captial name' do
expect(user_name).to eq('AARAV SHARMA')
end
end
end
Run a specific test,
rspec spec/controllers/systems_controller_spec.rb -e 'should display list of audit trails related to system'
RSpec hook - before, around etc.
Comments
Post a Comment
Let me know for any query..