File: lifecycle_spec.rb

package info (click to toggle)
ruby-delayed-job 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 388 kB
  • sloc: ruby: 2,780; makefile: 8
file content (75 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'helper'

describe Delayed::Lifecycle do
  let(:lifecycle) { Delayed::Lifecycle.new }
  let(:callback) { lambda { |*_args| } }
  let(:arguments) { [1] }
  let(:behavior) { double(Object, :before! => nil, :after! => nil, :inside! => nil) }
  let(:wrapped_block) { proc { behavior.inside! } }

  describe 'before callbacks' do
    before(:each) do
      lifecycle.before(:execute, &callback)
    end

    it 'executes before wrapped block' do
      expect(callback).to receive(:call).with(*arguments).ordered
      expect(behavior).to receive(:inside!).ordered
      lifecycle.run_callbacks :execute, *arguments, &wrapped_block
    end
  end

  describe 'after callbacks' do
    before(:each) do
      lifecycle.after(:execute, &callback)
    end

    it 'executes after wrapped block' do
      expect(behavior).to receive(:inside!).ordered
      expect(callback).to receive(:call).with(*arguments).ordered
      lifecycle.run_callbacks :execute, *arguments, &wrapped_block
    end
  end

  describe 'around callbacks' do
    before(:each) do
      lifecycle.around(:execute) do |*args, &block|
        behavior.before!
        block.call(*args)
        behavior.after!
      end
    end

    it 'wraps a block' do
      expect(behavior).to receive(:before!).ordered
      expect(behavior).to receive(:inside!).ordered
      expect(behavior).to receive(:after!).ordered
      lifecycle.run_callbacks :execute, *arguments, &wrapped_block
    end

    it 'executes multiple callbacks in order' do
      expect(behavior).to receive(:one).ordered
      expect(behavior).to receive(:two).ordered
      expect(behavior).to receive(:three).ordered

      lifecycle.around(:execute) do |*args, &block|
        behavior.one
        block.call(*args)
      end
      lifecycle.around(:execute) do |*args, &block|
        behavior.two
        block.call(*args)
      end
      lifecycle.around(:execute) do |*args, &block|
        behavior.three
        block.call(*args)
      end
      lifecycle.run_callbacks(:execute, *arguments, &wrapped_block)
    end
  end

  it 'raises if callback is executed with wrong number of parameters' do
    lifecycle.before(:execute, &callback)
    expect { lifecycle.run_callbacks(:execute, 1, 2, 3) {} }.to raise_error(ArgumentError, /1 parameter/)
  end
end