Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create destroy after hook specs #66

Merged
merged 2 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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