clockwork-mocks 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -4
- data/lib/clockwork_mocks/clockwork_task.rb +3 -3
- data/lib/clockwork_mocks/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1bad1a54175dbed4dbe8530c589ca6d2d699404
|
4
|
+
data.tar.gz: e515fa9fc4bea5f981a1b410115c55d0e6dd7816
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efd69b2649a2d37862af8c01ecd32df2b282bfa3c05aceab75395a2a1a532bfba2fbc88f2df869285790cf3a55c4d0663f8aacb31858476b9b1c64b498c8c771
|
7
|
+
data.tar.gz: 55019f777ec301c4269d595ff217634ab013f1ea197ed7c2c42173db7a7c766855de188eb8161642515c07a4f21bf31a8d70bb68b97927c5a69d2f88d7d72796
|
data/README.md
CHANGED
@@ -4,12 +4,52 @@ This gem provides **helpers for integration testing with clockwork**.
|
|
4
4
|
[Clockwork](https://github.com/Rykian/clockwork) provides a cron-like utility for ruby.
|
5
5
|
It works especially well in combination with [timecop](https://github.com/travisjeffery/timecop).
|
6
6
|
|
7
|
+
## Example Usecase
|
8
|
+
|
9
|
+
Image you have a rails app with a clockwork task scheduled every night to check for inactive users and sends them reminders via email.
|
10
|
+
Your `clock.rb` file would look as follows:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
module Clockwork
|
14
|
+
every(1.day, 'inactives', at: '00:00') do
|
15
|
+
User.inactive.each do
|
16
|
+
Mailer.reminder_mail.deliver_later
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
By using ClockworkMocks in combination with RSpec and Timecop, you could test this as follows:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
RSpec.describe ... do
|
26
|
+
before { ClockworkMocks.reset_rspec(method(:allow), method(:receive)) }
|
27
|
+
let!(:user) { create(:user) }
|
28
|
+
|
29
|
+
context 'after 7 days without action' do
|
30
|
+
before do
|
31
|
+
Timecop.freeze 7.days.from_now
|
32
|
+
ClockworkMocks.work
|
33
|
+
end
|
34
|
+
after { Timecop.return }
|
35
|
+
|
36
|
+
it 'should have sent the user a reminder' do
|
37
|
+
expect(ActionMailer::Base.deliveries).not_to be_empty
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
Note that this does not replace proper unit tests, but gives you the possibility to additionally test your system as a whole.
|
44
|
+
|
7
45
|
## Installation
|
8
46
|
|
9
|
-
Add this
|
47
|
+
Add this to your application's Gemfile:
|
10
48
|
|
11
49
|
```ruby
|
12
|
-
|
50
|
+
group :test do
|
51
|
+
gem 'clockwork-mocks'
|
52
|
+
end
|
13
53
|
```
|
14
54
|
|
15
55
|
And then execute:
|
@@ -48,11 +88,11 @@ Using this interface, you can use any stub provider to stub `Clockwork`'s method
|
|
48
88
|
For example with rspec-mock:
|
49
89
|
|
50
90
|
```ruby
|
51
|
-
allow
|
91
|
+
allow(Clockwork).to receive(:handler) do |&block|
|
52
92
|
ClockworkMocks.scheduler.handler(&block)
|
53
93
|
end
|
54
94
|
|
55
|
-
allow
|
95
|
+
allow(Clockwork).to receive(:every) do |interval, name, hash, &block|
|
56
96
|
ClockworkMocks.scheduler.every interval, name, hash, &block
|
57
97
|
end
|
58
98
|
|
@@ -91,6 +131,17 @@ not so often
|
|
91
131
|
often
|
92
132
|
```
|
93
133
|
|
134
|
+
### Sidekiq
|
135
|
+
|
136
|
+
If you use clockwork to schedule sidekiq jobs but want them to actually execute during integration testing I recommend one of two options:
|
137
|
+
|
138
|
+
1. Use sidekiq's inline mode to execute all jobs immediately during integration testing.
|
139
|
+
This works well if you don't on scheduled jobs or don't care about the execution date.
|
140
|
+
2. Use a library like [sidekiq-fake-scheduler](https://github.com/dpoetzsch/sidekiq-fake-scheduler).
|
141
|
+
This is superior to inline mode in that it respects the dates when the jobs are scheduled so you can test your application more fine-grained.
|
142
|
+
The proposed gem neatly integrates with clockwork-mocks.
|
143
|
+
Disclaimer: I am the creator and maintainer of sidekiq-fake-scheduler.
|
144
|
+
|
94
145
|
## Development
|
95
146
|
|
96
147
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -12,7 +12,7 @@ module ClockworkMocks
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def reset!
|
15
|
-
@due = Time.now
|
15
|
+
@due = Time.now.utc
|
16
16
|
@due = calc_due
|
17
17
|
end
|
18
18
|
|
@@ -20,7 +20,7 @@ module ClockworkMocks
|
|
20
20
|
if @block
|
21
21
|
@block.call
|
22
22
|
elsif handler
|
23
|
-
handler.call(@name, Time.now)
|
23
|
+
handler.call(@name, Time.now.utc)
|
24
24
|
end
|
25
25
|
@due = calc_due
|
26
26
|
end
|
@@ -30,7 +30,7 @@ module ClockworkMocks
|
|
30
30
|
private
|
31
31
|
|
32
32
|
def calc_due
|
33
|
-
ndue = Time.at((due || Time.now).to_f + @interval)
|
33
|
+
ndue = Time.at((due || Time.now.utc).to_f + @interval).utc
|
34
34
|
|
35
35
|
return ndue unless @hash[:at]
|
36
36
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clockwork-mocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Poetzsch-Heffter
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: clockwork
|
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: '0'
|
154
154
|
requirements: []
|
155
155
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.5.2
|
156
|
+
rubygems_version: 2.5.2.1
|
157
157
|
signing_key:
|
158
158
|
specification_version: 4
|
159
159
|
summary: Helpers for manual scheduling of clockwork tasks for integration testing
|