Add a spec for DryRunnable#dry_run! with event

Akinori MUSHA 9 anos atrás
pai
commit
55e0da91cd
1 arquivos alterados com 46 adições e 17 exclusões
  1. 46 17
      spec/concerns/dry_runnable_spec.rb

+ 46 - 17
spec/concerns/dry_runnable_spec.rb

@@ -7,10 +7,22 @@ describe DryRunnable do
7 7
     can_dry_run!
8 8
 
9 9
     def check
10
+      perform
11
+    end
12
+
13
+    def receive(events)
14
+      events.each do |event|
15
+        perform(event.payload['prefix'])
16
+      end
17
+    end
18
+
19
+    private
20
+
21
+    def perform(prefix = nil)
10 22
       log "Logging"
11
-      create_event payload: { 'test' => 'foo' }
23
+      create_event payload: { 'test' => "#{prefix}foo" }
12 24
       error "Recording error"
13
-      create_event payload: { 'test' => 'bar' }
25
+      create_event payload: { 'test' => "#{prefix}bar" }
14 26
       self.memory = { 'last_status' => 'ok', 'dry_run' => dry_run? }
15 27
       save!
16 28
     end
@@ -46,21 +58,6 @@ describe DryRunnable do
46 58
     expect(messages).to eq(['Logging', 'Recording error'])
47 59
   end
48 60
 
49
-  it "traps logging, event emission and memory updating, with dry_run? returning true" do
50
-    results = nil
51
-
52
-    expect {
53
-      results = @agent.dry_run!
54
-      @agent.reload
55
-    }.not_to change {
56
-      [@agent.memory, counts]
57
-    }
58
-
59
-    expect(results[:log]).to match(/\AI, .+ INFO -- : Logging\nE, .+ ERROR -- : Recording error\n/)
60
-    expect(results[:events]).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
61
-    expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
62
-  end
63
-
64 61
   it "does not perform dry-run if Agent does not support dry-run" do
65 62
     stub(@agent).can_dry_run? { false }
66 63
 
@@ -77,4 +74,36 @@ describe DryRunnable do
77 74
     expect(results[:events]).to eq([])
78 75
     expect(results[:memory]).to eq({})
79 76
   end
77
+
78
+  describe "dry_run!" do
79
+    it "traps any destructive operations during a run" do
80
+      results = nil
81
+
82
+      expect {
83
+        results = @agent.dry_run!
84
+        @agent.reload
85
+      }.not_to change {
86
+        [@agent.memory, counts]
87
+      }
88
+
89
+      expect(results[:log]).to match(/\AI, .+ INFO -- : Logging\nE, .+ ERROR -- : Recording error\n/)
90
+      expect(results[:events]).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
91
+      expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
92
+    end
93
+
94
+    it "traps any destructive operations during a run when an event is given" do
95
+      results = nil
96
+
97
+      expect {
98
+        results = @agent.dry_run!(Event.new(payload: { 'prefix' => 'super' }))
99
+        @agent.reload
100
+      }.not_to change {
101
+        [@agent.memory, counts]
102
+      }
103
+
104
+      expect(results[:log]).to match(/\AI, .+ INFO -- : Logging\nE, .+ ERROR -- : Recording error\n/)
105
+      expect(results[:events]).to eq([{ 'test' => 'superfoo' }, { 'test' => 'superbar' }])
106
+      expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
107
+    end
108
+  end
80 109
 end