clockwork-mocks 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -27
- data/bin/console +1 -1
- data/lib/clockwork_mocks.rb +18 -0
- data/lib/clockwork_mocks/clockwork_task.rb +1 -1
- data/lib/clockwork_mocks/scheduler.rb +6 -6
- data/lib/clockwork_mocks/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17b8f640f1ade961325aa2a3f09024a14afc5eea
|
4
|
+
data.tar.gz: acca7e106a0e1e1fef75a3d3da5c2fdf2c91a294
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 736060ca732b91939c22dd0f7df7fd3c898111de6ddc4acdfab8c2256fe4457b3718b1baa768bb34682b5c7d0af564396d8923ef8a3bef34b37bc139321accf6
|
7
|
+
data.tar.gz: 432f1b04c841fd621d6ca6b2df0adf95aa38213f4b9bb1521a17f7e1d407e7db25cfca9c4f100cf9c06aba5f6037cdfaa847dd1902952035b17a911d76d4c316
|
data/README.md
CHANGED
@@ -26,42 +26,35 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
RSpec.describe ... do
|
29
|
-
|
29
|
+
before(:all) { ClockworkMocks.init_rspec(method(:allow), method(:receive), 'path/to/clock.rb') }
|
30
|
+
before { ClockworkMocks.reset! }
|
30
31
|
end
|
31
32
|
```
|
32
33
|
|
33
|
-
If you do not pass a clock file path to `ClockworkMocks
|
34
|
-
This reloads the `clock.rb` file in every test.
|
35
|
-
If you care about that performance leak, there is a more verbose initialization option:
|
36
|
-
|
37
|
-
```ruby
|
38
|
-
RSpec.describe ... do
|
39
|
-
clockwork_scheduler = nil
|
40
|
-
|
41
|
-
before do
|
42
|
-
clockwork_scheduler ||= ClockworkMocks::Scheduler.init_rspec(metod(:allow), method(:receive), 'path/to/clock.rb')
|
43
|
-
clockwork_scheduler.reset!
|
44
|
-
end
|
45
|
-
end
|
46
|
-
```
|
34
|
+
If you do not pass a clock file path to `ClockworkMocks.init_rspec` and you are in a rails environment it will assume `"#{Rails.root}/clock.rb"` by default.
|
47
35
|
|
48
36
|
### General Initialization
|
49
37
|
|
50
38
|
```ruby
|
51
|
-
|
52
|
-
|
53
|
-
|
39
|
+
ClockworkMocks.scheduler.handler do |job, time|
|
40
|
+
# something
|
41
|
+
end
|
54
42
|
|
55
|
-
|
43
|
+
ClockworkMocks.scheduler.every(1.day, 'some task', at: '23:00') do
|
56
44
|
# something
|
57
45
|
end
|
58
46
|
```
|
59
47
|
|
60
|
-
Using this interface, you can use any stub provider to stub `Clockwork
|
48
|
+
Using this interface, you can use any stub provider to stub `Clockwork`'s methods and call `ClockworkMocks.scheduler`'s methods instead.
|
49
|
+
For example with rspec-mock:
|
61
50
|
|
62
51
|
```ruby
|
52
|
+
allow.call(Clockwork).to receive.call(:handler) do |&block|
|
53
|
+
ClockworkMocks.scheduler.handler(&block)
|
54
|
+
end
|
55
|
+
|
63
56
|
allow.call(Clockwork).to receive.call(:every) do |interval, name, hash, &block|
|
64
|
-
|
57
|
+
ClockworkMocks.scheduler.every interval, name, hash, &block
|
65
58
|
end
|
66
59
|
|
67
60
|
load 'path/to/clock.rb'
|
@@ -69,12 +62,12 @@ load 'path/to/clock.rb'
|
|
69
62
|
|
70
63
|
### Executing clockwork tasks
|
71
64
|
|
72
|
-
At any time you can call `
|
65
|
+
At any time you can call `ClockworkMocks.work` to execute all tasks that are due.
|
73
66
|
This works especially well in combination with timecop (although the latter is not a requirement):
|
74
67
|
|
75
68
|
```ruby
|
76
69
|
Timecop.freeze(2.days.from_now) do
|
77
|
-
|
70
|
+
ClockworkMocks.work
|
78
71
|
end
|
79
72
|
```
|
80
73
|
|
@@ -82,11 +75,11 @@ Tasks will be executed in correct order.
|
|
82
75
|
If enough time passed, tasks will be executed multiple times:
|
83
76
|
|
84
77
|
```ruby
|
85
|
-
|
86
|
-
|
78
|
+
ClockworkMocks.scheduler.every(1.second, 'often') { puts 'often' }
|
79
|
+
ClockworkMocks.scheduler.every(2.seconds, 'not-so-often') { puts 'not so often' }
|
87
80
|
|
88
81
|
Timecop.freeze(3.seconds.from_now) do
|
89
|
-
|
82
|
+
ClockworkMocks.work
|
90
83
|
end
|
91
84
|
```
|
92
85
|
|
@@ -95,7 +88,7 @@ outputs
|
|
95
88
|
```
|
96
89
|
often
|
97
90
|
often
|
98
|
-
not
|
91
|
+
not so often
|
99
92
|
often
|
100
93
|
```
|
101
94
|
|
data/bin/console
CHANGED
data/lib/clockwork_mocks.rb
CHANGED
@@ -5,3 +5,21 @@ require 'clockwork'
|
|
5
5
|
require 'clockwork_mocks/version'
|
6
6
|
require 'clockwork_mocks/clockwork_task'
|
7
7
|
require 'clockwork_mocks/scheduler'
|
8
|
+
|
9
|
+
module ClockworkMocks
|
10
|
+
def self.init_rspec(allow, receive, clock_file = nil, &block)
|
11
|
+
scheduler.init_rspec(allow, receive, clock_file, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.scheduler
|
15
|
+
@scheduler ||= Scheduler.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.reset!
|
19
|
+
scheduler.reset!
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.work
|
23
|
+
scheduler.work
|
24
|
+
end
|
25
|
+
end
|
@@ -8,15 +8,17 @@ module ClockworkMocks
|
|
8
8
|
@tasks = []
|
9
9
|
end
|
10
10
|
|
11
|
-
def self.init_rspec(allow, receive, clock_file = nil)
|
12
|
-
|
11
|
+
def self.init_rspec(allow, receive, clock_file = nil, &block)
|
12
|
+
Scheduler.new.tap { |s| s.init_rspec(allow, receive, clock_file, &block) }
|
13
|
+
end
|
13
14
|
|
15
|
+
def init_rspec(allow, receive, clock_file = nil)
|
14
16
|
allow.call(Clockwork).to receive.call(:handler) do |&block|
|
15
|
-
|
17
|
+
handler(&block)
|
16
18
|
end
|
17
19
|
|
18
20
|
allow.call(Clockwork).to receive.call(:every) do |interval, name, hash, &block|
|
19
|
-
|
21
|
+
every interval, name, hash, &block
|
20
22
|
end
|
21
23
|
|
22
24
|
if block_given?
|
@@ -29,8 +31,6 @@ module ClockworkMocks
|
|
29
31
|
|
30
32
|
load clock_file if clock_file
|
31
33
|
end
|
32
|
-
|
33
|
-
scheduler
|
34
34
|
end
|
35
35
|
|
36
36
|
def work
|