Skip to content

Commit

Permalink
Merge pull request #66 from sungwoncho/test/add-create-destroy-specs
Browse files Browse the repository at this point in the history
Add create destroy after hook specs
  • Loading branch information
chevinbrown authored Dec 8, 2018
2 parents 3c9798c + a6ae8cb commit 17ed49f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/has_friendship/friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Friendship < ActiveRecord::Base
end

after_destroy do |record|
friend.try(:on_friendship_destroyed, record)
friendable.try(:on_friendship_destroyed, record)
end

enum status: { pending: 0, requested: 1, accepted: 2, blocked: 3 } do
Expand Down
28 changes: 24 additions & 4 deletions spec/has_friendship/friendable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require 'rails_helper'

describe User, focus: true do

let(:user){ User.create(name: 'Jessie') }
let(:friend){ User.create(name: 'Heisenberg') }
describe User do
let(:user) { User.create(name: 'Jessie') }
let(:friend) { User.create(name: 'Heisenberg') }

describe "association" do
# TODO: find a way to test condition
Expand Down Expand Up @@ -297,5 +296,26 @@
end
end

describe '#on_friendship_destroyed' do
context 'when friendship is destroyed' do
it 'should be called' do
user.friend_request(friend)
friendship = find_friendship_record(user, friend)
expect(friendship.friendable).to receive(:on_friendship_destroyed)
friendship.destroy
end
end
end

describe '#on_friendship_created' do
context 'when friendship is created' do
it 'should be called' do
user.friend_request(friend) do
friendship = find_friendship_record(user, friend)
expect(friendship.try(:friendable)).to receive(:on_friendship_created)
end
end
end
end
end
end

0 comments on commit 17ed49f

Please sign in to comment.