Skip to content

Commit 84a820d

Browse files
committed
fix expect_inputs and promise_outputs with non-Interactify classes
1 parent 04b846c commit 84a820d

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

lib/interactify/rspec/matchers.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
RSpec::Matchers.define :expect_inputs do |*expected_inputs|
88
match do |actual|
9-
actual_inputs = actual.expected_keys
9+
next false unless actual.respond_to?(:expected_keys)
10+
11+
actual_inputs = Array(actual.expected_keys)
1012
@missing_inputs = expected_inputs - actual_inputs
1113
@extra_inputs = actual_inputs - expected_inputs
1214

@@ -25,7 +27,9 @@
2527
# e.g. expect(described_class).to promise_outputs(:request_logger)
2628
RSpec::Matchers.define :promise_outputs do |*expected_outputs|
2729
match do |actual|
28-
actual_outputs = actual.promised_keys
30+
next false unless actual.respond_to?(:promised_keys)
31+
32+
actual_outputs = Array(actual.promised_keys)
2933
@missing_outputs = expected_outputs - actual_outputs
3034
@extra_outputs = actual_outputs - expected_outputs
3135

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'interactify/rspec/matchers'
2+
3+
RSpec.describe 'rspec matchers' do
4+
describe 'expect_inputs' do
5+
it 'passes when the inputs are correct' do
6+
expect(k(:A)).to expect_inputs(:a)
7+
expect(k(:A)).not_to expect_inputs(:b)
8+
9+
expect(k(:B)).to expect_inputs(:b)
10+
expect(k(:B)).not_to expect_inputs(:a)
11+
12+
expect(k(:P)).not_to expect_inputs(:a, :b)
13+
end
14+
15+
it 'works with vanilla interactors without blowing up' do
16+
expect(k(:Vanilla)).not_to expect_inputs(:a)
17+
end
18+
end
19+
20+
describe 'promise_outputs' do
21+
it 'passes when the outputs are correct' do
22+
expect(k(:A)).not_to promise_outputs(:a)
23+
expect(k(:A)).not_to promise_outputs(:a)
24+
25+
expect(k(:P)).to promise_outputs(:a, :b, :c)
26+
end
27+
28+
it 'works with vanilla interactors without blowing up' do
29+
expect(k(:Vanilla)).not_to promise_outputs(:a)
30+
end
31+
end
32+
33+
34+
module self::SomeNamespace
35+
Vanilla = Class.new do
36+
include Interactor
37+
end
38+
39+
A = Class.new do
40+
include Interactify
41+
expect :a
42+
end
43+
44+
B = Class.new do
45+
include Interactify
46+
expect :b
47+
end
48+
49+
P = Class.new do
50+
include Interactify
51+
promise :a, :b, :c
52+
end
53+
end
54+
55+
def k(klass)
56+
self.class::SomeNamespace.const_get(klass)
57+
end
58+
end

0 commit comments

Comments
 (0)