@@ -36,31 +36,36 @@ module Pact
36
36
37
37
let ( :consumer_contract_writer ) { ConsumerContractWriter . new ( consumer_contract_details , logger ) }
38
38
39
- describe "consumer_contract" do
40
-
41
- let ( :subject ) { consumer_contract_writer . consumer_contract }
39
+ describe "#write" do
40
+ it "writes the pact file to the pact_dir" do
41
+ FileUtils . rm_rf target_pact_file_location
42
+ consumer_contract_writer . write
43
+ expect ( File . exist? ( target_pact_file_location ) ) . to be true
44
+ end
42
45
43
46
context "when overwriting pact" do
44
47
45
48
it "it uses only the interactions from the current test run" do
46
- expect ( consumer_contract_writer . consumer_contract . interactions ) . to eq new_interactions
49
+ consumer_contract_writer . write
50
+ expect ( Pact ::ConsumerContract . from_json ( File . read ( target_pact_file_location ) ) . interactions . collect ( &:to_hash ) ) . to eq new_interactions . collect ( &:to_hash )
47
51
end
48
52
49
53
end
50
54
51
55
context "when updating pact" do
52
56
53
- let ( :pactfile_write_mode ) { :update }
57
+ let ( :pactfile_write_mode ) { :update }
54
58
55
59
it "merges the interactions from the current test run with the interactions from the existing file" do
56
60
allow_any_instance_of ( ConsumerContractWriter ) . to receive ( :info_and_puts )
57
- expect ( consumer_contract_writer . consumer_contract . interactions ) . to eq existing_interactions + new_interactions
61
+ consumer_contract_writer . write
62
+ expect ( Pact ::ConsumerContract . from_uri ( target_pact_file_location ) . interactions . size ) . to eq 3
58
63
end
59
64
60
- let ( :line0 ) { / \* / }
61
- let ( :line1 ) { / Updating existing file/ }
62
- let ( :line2 ) { / Only interactions defined in this test run will be updated/ }
63
- let ( :line3 ) { / As interactions are identified by description and provider state/ }
65
+ let ( :line0 ) { "*" * 77 }
66
+ let ( :line1 ) { %r{ Updating existing file} }
67
+ let ( :line2 ) { %r{ Only interactions defined in this test run will be updated} }
68
+ let ( :line3 ) { %r{ As interactions are identified by description and provider state} }
64
69
65
70
it "logs a description message" do
66
71
expect ( $stdout) . to receive ( :puts ) . with ( line0 ) . twice
@@ -71,40 +76,37 @@ module Pact
71
76
expect ( logger ) . to receive ( :info ) . with ( line1 )
72
77
expect ( logger ) . to receive ( :info ) . with ( line2 )
73
78
expect ( logger ) . to receive ( :info ) . with ( line3 )
74
- consumer_contract_writer . consumer_contract
79
+ consumer_contract_writer . write
75
80
end
76
81
end
77
82
78
83
context "when an error occurs deserializing the existing pactfile" do
79
84
80
- let ( :pactfile_write_mode ) { :update }
85
+ let ( :pactfile_write_mode ) { :update }
81
86
let ( :error ) { RuntimeError . new ( 'some error' ) }
82
- let ( :line1 ) { / Could not load existing consumer contract from .* due to some error/ }
87
+ let ( :line1 ) { %r{ Could not load existing consumer contract from .* due to} }
83
88
84
89
before do
85
- allow ( ConsumerContract ) . to receive ( :from_json ) . and_raise ( error )
90
+ File . open ( target_pact_file_location , "w" ) { |file | file << "wiffle" }
91
+
86
92
allow ( $stderr) . to receive ( :puts )
87
93
allow ( logger ) . to receive ( :puts )
88
94
end
89
95
96
+ after do
97
+ FileUtils . rm ( target_pact_file_location )
98
+ end
99
+
90
100
it "logs the error" do
91
101
expect ( $stderr) . to receive ( :puts ) . with ( line1 )
92
102
expect ( logger ) . to receive ( :warn ) . with ( line1 )
93
- consumer_contract_writer . consumer_contract
103
+ consumer_contract_writer . write
94
104
end
95
105
96
106
it "uses the new interactions" do
97
107
expect ( consumer_contract_writer . consumer_contract . interactions ) . to eq new_interactions
98
108
end
99
109
end
100
- end
101
-
102
- describe "#write" do
103
- it "writes the pact file to the pact_dir" do
104
- FileUtils . rm_rf target_pact_file_location
105
- consumer_contract_writer . write
106
- expect ( File . exist? ( target_pact_file_location ) ) . to be true
107
- end
108
110
109
111
context "when pactfile_write_mode is update" do
110
112
let ( :pactfile_write_mode ) { :update }
0 commit comments