callback methos rspecs in ruby on rails

Add gem 'shoulda-callback-matchers'

In app/models/user.rb

class User < ActiveRecord::Base
  after_create :create_usersetting
  before_destroy :delete_user_information
end


In spec/models/user_spec.rb

 require "rails_helper"
 require 'factory_girl_rails'
describe User do

  before(:each) do
    @user = User.create(:email=>"example@gmail.com",:password=>"1234578")
  end

it "should trigger create_usersetting on after create" do
     should callback(:create_usersetting).after(:create)   
    end
  
    it  "should trigger delete_user_information on before destroy" do
      is_expected.to callback(:delete_user_information).before(:destroy)
    end

end

Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Reading Excel Sheets using "Roo" gem in ruby on rails

Bootstrap Select Picker append add new item if search not exist

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails