/
rails-callback_log_spec.rb
72 lines (63 loc) · 2.98 KB
/
rails-callback_log_spec.rb
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
require "spec_helper"
module ActiveSupport
module Callbacks
if ::ActiveSupport.gem_version >= ::Gem::Version.new("5.1.0")
RSpec.describe CallTemplate do
describe "#make_lambda" do
it "returns a Proc that calls the rails logger" do
callback = double("mock callback")
cb = described_class.build(:banana, callback)
# Expect `make_lambda` to return a `Proc`.
lambda_with_log = cb.make_lambda
expect(lambda_with_log).to be_a(::Proc)
# Mock the rails logger. We're going to expect it to receive a `debug` message.
logger = double
allow(logger).to receive(:debug)
allow(::RailsCallbackLog).to receive(:logger).and_return(logger)
target = double
# Symbol-based callbacks have a `target` and a `value`. The definition of
# these arguments is not clear to me yet. My guess is an AR callback's `target`
# would be a model instance, for example.
allow(target).to receive(:kiwi).with(:banana)
allow(target).to receive(:banana)
# Call our lambda and expect it to send a debug message to our mock logger.
lambda_with_log.call(target, :kiwi)
expect(logger).to have_received(:debug).once.with("Callback: banana")
expect(target).to have_received(:banana).once
end
end
end
else
RSpec.describe Callback do
describe "#make_lambda" do
it "returns a Proc that calls the rails logger" do
# Instantiate a `Callback`. For this test, none of the arguments matter.
name = double.as_null_object
filter = double.as_null_object
kind = double.as_null_object
options = double.as_null_object
chain_config = double.as_null_object
cb = described_class.new(name, filter, kind, options, chain_config)
# Expect `make_lambda` to return a `Proc`.
lambda_with_log = cb.make_lambda(:banana)
expect(lambda_with_log).to be_a(::Proc)
# Mock the rails logger. We're going to expect it to receive a `debug` message.
logger = double
allow(logger).to receive(:debug)
allow(::Rails).to receive(:logger).and_return(logger)
target = double
# Symbol-based callbacks have a `target` and a `value`. The definition of
# these arguments is not clear to me yet. My guess is an AR callback's `target`
# would be a model instance, for example.
allow(target).to receive(:kiwi).with(:banana)
allow(target).to receive(:banana)
# Call our lambda and expect it to send a debug message to our mock logger.
lambda_with_log.call(target, :kiwi)
expect(logger).to have_received(:debug).once.with("Callback: banana")
expect(target).to have_received(:banana).once
end
end
end
end
end
end