rspec-activejob 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8206ef4115ccb0283a36013a2b2bc43d73c3285
4
+ data.tar.gz: d001cf73f04e980b58d725a593807f4b80f826aa
5
+ SHA512:
6
+ metadata.gz: 2aba60f994f30bdaa7099c0388674642961a98ff04da4caee1c801ce2f3846710a5be7b78e96b190ef6c4763256a69a4c96c4303c67f06b41e48e840d6e76181
7
+ data.tar.gz: c65123947482b545cc68c4cadfa8844afd219631231f087f859a98f914bcbca8515f107cee91c5d0c0350b8a4c2a75fa5565090ae1d5e90a10c3990a95148175
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'pry'
7
+ end
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rspec-activejob (0.0.1)
5
+ activejob (>= 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activejob (4.2.0)
11
+ activesupport (= 4.2.0)
12
+ globalid (>= 0.3.0)
13
+ activesupport (4.2.0)
14
+ i18n (~> 0.7)
15
+ json (~> 1.7, >= 1.7.7)
16
+ minitest (~> 5.1)
17
+ thread_safe (~> 0.3, >= 0.3.4)
18
+ tzinfo (~> 1.1)
19
+ coderay (1.1.0)
20
+ diff-lcs (1.2.5)
21
+ globalid (0.3.0)
22
+ activesupport (>= 4.1.0)
23
+ i18n (0.7.0)
24
+ json (1.8.2)
25
+ method_source (0.8.2)
26
+ minitest (5.5.1)
27
+ pry (0.10.1)
28
+ coderay (~> 1.1.0)
29
+ method_source (~> 0.8.1)
30
+ slop (~> 3.4)
31
+ rspec (3.1.0)
32
+ rspec-core (~> 3.1.0)
33
+ rspec-expectations (~> 3.1.0)
34
+ rspec-mocks (~> 3.1.0)
35
+ rspec-core (3.1.7)
36
+ rspec-support (~> 3.1.0)
37
+ rspec-expectations (3.1.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.1.0)
40
+ rspec-its (1.1.0)
41
+ rspec-core (>= 3.0.0)
42
+ rspec-expectations (>= 3.0.0)
43
+ rspec-mocks (3.1.3)
44
+ rspec-support (~> 3.1.0)
45
+ rspec-support (3.1.2)
46
+ slop (3.6.0)
47
+ thread_safe (0.3.4)
48
+ tzinfo (1.2.2)
49
+ thread_safe (~> 0.1)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ pry
56
+ rspec
57
+ rspec-activejob!
58
+ rspec-its
@@ -0,0 +1,15 @@
1
+ # RSpec ActiveJob matchers
2
+
3
+ ```ruby
4
+ RSpec.configure do |config|
5
+ config.include(RSpec::ActiveJob)
6
+ end
7
+
8
+ RSpec.describe MyController do
9
+ let(:user) { create(:user) }
10
+ let(:params) { { user_id: user.id } }
11
+ subject(:make_request) { described_class.make_request(params) }
12
+
13
+ specify { expect { make_request }.to enqueue_a(RequestMaker).with(user) }
14
+ end
15
+ ```
@@ -0,0 +1,8 @@
1
+ require 'rspec/active_job/matchers'
2
+ module RSpec
3
+ module ActiveJob
4
+ def enqueue_a(job_class)
5
+ Matchers::EnqueueA.new(job_class)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,75 @@
1
+ module RSpec
2
+ module ActiveJob
3
+ module Matchers
4
+ class EnqueueA
5
+ def initialize(job_class = nil)
6
+ @job_class = job_class
7
+ end
8
+
9
+ def matches?(block)
10
+ @before_jobs = enqueued_jobs.dup
11
+ block.call
12
+ enqueued_something? && enqueued_correct_class? && with_correct_args?
13
+ end
14
+
15
+ def with(*args)
16
+ raise "Must specify the job class when specifying arguments" unless job_class
17
+
18
+ @expected_args = args
19
+ self
20
+ end
21
+
22
+ def failure_message
23
+ unless enqueued_something?
24
+ return "expected to enqueue a #{job_class || 'job'}, enqueued nothing"
25
+ end
26
+
27
+ unless enqueued_correct_class?
28
+ return "expected to enqueue a #{job_class}, enqueued a #{enqueued_jobs.last[:job]}"
29
+ end
30
+
31
+ "expected to enqueue a #{job_class} with #{expected_args}, but enqueued with " \
32
+ "#{new_jobs_with_correct_class.first[:args]}"
33
+ end
34
+
35
+ def supports_block_expectations?
36
+ true
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :before_count, :after_count, :job_class, :expected_args
42
+
43
+ def enqueued_something?
44
+ new_jobs.any?
45
+ end
46
+
47
+ def enqueued_correct_class?
48
+ return true unless job_class
49
+ new_jobs_with_correct_class.any?
50
+ end
51
+
52
+ def with_correct_args?
53
+ return true unless expected_args
54
+ new_jobs_with_correct_class_and_args.any?
55
+ end
56
+
57
+ def new_jobs
58
+ enqueued_jobs - @before_jobs
59
+ end
60
+
61
+ def new_jobs_with_correct_class
62
+ new_jobs.select { |job| job[:job] == job_class }
63
+ end
64
+
65
+ def new_jobs_with_correct_class_and_args
66
+ new_jobs_with_correct_class.select { |job| job[:args] == expected_args }
67
+ end
68
+
69
+ def enqueued_jobs
70
+ ::ActiveJob::Base.queue_adapter.enqueued_jobs
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ module RSpec
2
+ module ActiveJob
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../lib/rspec/active_job/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'rspec-activejob'
5
+ s.version = RSpec::ActiveJob::VERSION
6
+ s.date = Date.today.strftime('%Y-%m-%d')
7
+ s.authors = ['Isaac Seymour']
8
+ s.email = ['isaac@isaacseymour.co.uk']
9
+ s.summary = 'RSpec matchers to test ActiveJob'
10
+ s.description = <<-EOL
11
+ RSpec matchers for ActiveJob:
12
+ * expect { method }.to enqueue_a(MyJob).with('some', 'arguments')
13
+ EOL
14
+ s.homepage = 'http://github.com/gocardless/rspec-activejob'
15
+ s.license = 'MIT'
16
+
17
+ s.has_rdoc = false
18
+ s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
19
+ s.require_paths = %w(lib)
20
+
21
+ s.add_runtime_dependency('activejob', '>= 4.2')
22
+
23
+ s.add_development_dependency('rspec')
24
+ s.add_development_dependency('rspec-its')
25
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RSpec::ActiveJob::Matchers::EnqueueA do
4
+ class AJob; end
5
+ class BJob; end
6
+ let(:enqueued_jobs) { [] }
7
+ before do
8
+ allow(::ActiveJob::Base).
9
+ to receive(:queue_adapter).and_return(double(enqueued_jobs: enqueued_jobs))
10
+ end
11
+
12
+ let(:job_class) { nil }
13
+ let(:instance) { described_class.new(job_class) }
14
+ subject(:matches?) { instance.matches?(proc) }
15
+
16
+ context "when nothing gets enqueued" do
17
+ let(:proc) { -> {} }
18
+ it { is_expected.to be(false) }
19
+ specify do
20
+ matches?
21
+ expect(instance.failure_message).
22
+ to eq("expected to enqueue a job, enqueued nothing")
23
+ end
24
+ end
25
+
26
+ context "when something gets enqueued" do
27
+ let(:proc) { -> { enqueued_jobs << { job: AJob, args: [] } } }
28
+
29
+ it { is_expected.to be(true) }
30
+
31
+ context "when it enqueues the wrong job" do
32
+ let(:job_class) { BJob }
33
+
34
+ it { is_expected.to be(false) }
35
+ specify do
36
+ matches?
37
+ expect(instance.failure_message).
38
+ to eq("expected to enqueue a BJob, enqueued a AJob")
39
+ end
40
+ end
41
+
42
+ context "when it enqueues two jobs" do
43
+ let(:proc) do
44
+ -> { enqueued_jobs << { job: AJob, args: []} << { job: BJob, args: [] } }
45
+ end
46
+
47
+ it { is_expected.to be(true) }
48
+ end
49
+ end
50
+
51
+ context "with argument expectations" do
52
+ let(:job_class) { AJob }
53
+ let(:instance) { described_class.new(job_class).with(*arguments) }
54
+ let(:arguments) { ['thing', 'other_thing'] }
55
+
56
+ let(:proc) { -> { enqueued_jobs << { job: AJob, args: arguments } } }
57
+
58
+ it { is_expected.to be(true) }
59
+
60
+ context "with mismatching arguments" do
61
+ let(:proc) { -> { enqueued_jobs << { job: AJob, args: [] } } }
62
+
63
+ it { is_expected.to be(false) }
64
+ specify do
65
+ matches?
66
+ expect(instance.failure_message).
67
+ to eq("expected to enqueue a AJob with #{arguments}, but enqueued with []")
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,16 @@
1
+ require 'rspec/active_job'
2
+ require 'pry'
3
+
4
+ RSpec.configure do |config|
5
+ config.color = true
6
+ config.order = :random
7
+ config.disable_monkey_patching!
8
+ end
9
+
10
+ module ActiveJob
11
+ class Base
12
+ def self.queue_adapter
13
+ Struct.new(:enqueued_jobs).new(enqueued_jobs: [])
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-activejob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Isaac Seymour
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activejob
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-its
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |2
56
+ RSpec matchers for ActiveJob:
57
+ * expect { method }.to enqueue_a(MyJob).with('some', 'arguments')
58
+ email:
59
+ - isaac@isaacseymour.co.uk
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - README.md
67
+ - lib/rspec/active_job.rb
68
+ - lib/rspec/active_job/matchers.rb
69
+ - lib/rspec/active_job/version.rb
70
+ - rspec-activejob.gemspec
71
+ - spec/rspec/active_job/matchers_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/gocardless/rspec-activejob
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: RSpec matchers to test ActiveJob
97
+ test_files: []
98
+ has_rdoc: false