|
14 | 14 | )
|
15 | 15 | end
|
16 | 16 | let(:persister) { ::Persister::PostPersister.new(post) }
|
| 17 | + let(:event) { Fabricate.build(:event, id: 27) } |
17 | 18 |
|
18 | 19 | describe '#save' do
|
19 |
| - before { persister.save } |
20 |
| - |
21 | 20 | it 'saves the post' do
|
| 21 | + persister.save |
| 22 | + |
22 | 23 | expect(post).to be_persisted
|
23 | 24 | end
|
24 | 25 |
|
25 |
| - # TODO: write better expectation |
26 | 26 | it 'creates an event' do
|
27 |
| - expect(Event.where(post_id: post.id).first.action).to eq('created') |
| 27 | + expect(::Event).to receive(:create!).with(action: :created, post: post).and_return(event) |
| 28 | + |
| 29 | + persister.save |
| 30 | + end |
| 31 | + |
| 32 | + context 'background job' do |
| 33 | + before do |
| 34 | + ActiveJob::Base.queue_adapter = :test |
| 35 | + allow(::Event).to receive(:create!).and_return(event) |
| 36 | + persister.save |
| 37 | + end |
| 38 | + |
| 39 | + it 'enqueues a CreatePushNotificationsJob background job' do |
| 40 | + expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27) |
| 41 | + end |
28 | 42 | end
|
29 | 43 | end
|
30 | 44 |
|
31 | 45 | describe '#update_attributes' do
|
32 |
| - before { persister.update_attributes(title: 'New title') } |
33 |
| - |
34 | 46 | it 'updates the resource attributes' do
|
| 47 | + persister.update_attributes(title: 'New title') |
| 48 | + |
35 | 49 | expect(post.title).to eq('New title')
|
36 | 50 | end
|
37 | 51 |
|
38 |
| - # TODO: write better expectation |
39 | 52 | it 'creates an event' do
|
40 |
| - expect(Event.where(post_id: post.id).first.action).to eq('updated') |
| 53 | + expect(::Event).to receive(:create!).with(action: :updated, post: post).and_return(event) |
| 54 | + |
| 55 | + persister.update_attributes(title: 'New title') |
| 56 | + end |
| 57 | + |
| 58 | + context 'background job' do |
| 59 | + before do |
| 60 | + ActiveJob::Base.queue_adapter = :test |
| 61 | + allow(::Event).to receive(:create!).and_return(event) |
| 62 | + persister.update_attributes(title: 'New title') |
| 63 | + end |
| 64 | + |
| 65 | + it 'enqueues a CreatePushNotificationsJob background job' do |
| 66 | + expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27) |
| 67 | + end |
41 | 68 | end
|
42 | 69 | end
|
43 | 70 | end
|
0 commit comments