Thursday, 14 March 2019

How to write RSpec for Rails model?

RSpec for model

1. Add the below gem in your Gemfile and run "bundle install"
    gem 'rspec-rails' 

2. To generate spec folder run the below command
    rails generate rspec:install

3. To generate spec for specific model run the below command
    rails g rspec:model Address

4. To write the spec test cases, add the below codes in "spec/address_spec.rb"
RSpec.describe Address, type: :model do
  it 'Belongs to Builder' do
    add = Address.new
    builder = Builder.new
    builder.addresses << add
    expect(add.builder).to be builder
  end
end


5. To run spec for all 
    bundle exec rspec

6. To run spec for specific model
    bundle exec rspec spec/models/address_spec.rb

7. To run spec with html format output.
    rspec spec/models/address_spec.rb --format h > output.html
 


No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...