Skip to content

Commit

Permalink
Merge pull request #6 from stitchfix/support-multiple-rspecs
Browse files Browse the repository at this point in the history
Support RSpec 2 and RSpec 3 define matcher messages :shipit:
  • Loading branch information
simeonwillbanks committed Nov 19, 2015
2 parents 24f7ca5 + 6709a6c commit a515d9c
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ language: ruby
rvm:
- 2.1.0
- 2.2.0
- 2.0.0
- 1.9.3
- ruby-head
gemfile:
- Gemfile
- gemfiles/rspec2.gemfile
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ GEM
remote: https://www.rubygems.org/
specs:
diff-lcs (1.2.5)
fakeredis (0.5.0)
redis (~> 3.0)
mono_logger (1.1.0)
multi_json (1.11.2)
rack (1.6.4)
Expand Down Expand Up @@ -48,6 +50,7 @@ PLATFORMS
ruby

DEPENDENCIES
fakeredis
rake
resqutils!
rspec
Expand Down
7 changes: 7 additions & 0 deletions gemfiles/rspec2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://www.rubygems.org'

group :test, :development do
gem "rspec", "~> 2"
end

gemspec path: "../"
54 changes: 54 additions & 0 deletions gemfiles/rspec2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
PATH
remote: ../
specs:
resqutils (1.1.1)
resque

GEM
remote: https://www.rubygems.org/
specs:
diff-lcs (1.2.5)
fakeredis (0.5.0)
redis (~> 3.0)
mono_logger (1.1.0)
multi_json (1.11.2)
rack (1.6.4)
rack-protection (1.5.3)
rack
rake (10.4.2)
redis (3.2.1)
redis-namespace (1.5.2)
redis (~> 3.0, >= 3.0.4)
resque (1.25.2)
mono_logger (~> 1.0)
multi_json (~> 1.0)
redis-namespace (~> 1.3)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
rspec (2.99.0)
rspec-core (~> 2.99.0)
rspec-expectations (~> 2.99.0)
rspec-mocks (~> 2.99.0)
rspec-core (2.99.2)
rspec-expectations (2.99.2)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.99.4)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.1)
vegas (0.1.11)
rack (>= 1.0.0)

PLATFORMS
ruby

DEPENDENCIES
fakeredis
rake
resqutils!
rspec (~> 2)

BUNDLED WITH
1.10.6
16 changes: 14 additions & 2 deletions lib/resqutils/spec/resque_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
jobs_matching(queue_name,expected_job_hash).first.size == 1
end

failure_message do |queue_name|
failure_message_method = if defined?(failure_message)
:failure_message
else
:failure_message_for_should
end

send(failure_message_method) do |queue_name|
matching_jobs,all_jobs = jobs_matching(queue_name,expected_job_hash)
if matching_jobs.empty?
"No jobs in #{queue_name} matched #{expected_job_hash.inspect} (Found these jobs: #{all_jobs.map(&:inspect).join(',')})"
Expand All @@ -21,7 +27,13 @@
end
end

failure_message_when_negated do |queue_name|
failure_message_when_negated_method = if defined?(failure_message_when_negated)
:failure_message_when_negated
else
:failure_message_for_should_not
end

send(failure_message_when_negated_method) do |queue_name|
"Found job #{expected_job_hash.inspect} in the queue"
end
end
4 changes: 2 additions & 2 deletions lib/resqutils/stale_workers_killer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def self.perform
self.new.kill_stale_workers
end

def initialize(stale_workers: nil)
@stale_workers = stale_workers || Resqutils::StaleWorkers.new
def initialize(options={})
@stale_workers = options.fetch(:stale_workers, Resqutils::StaleWorkers.new)
end
def kill_stale_workers
@stale_workers.each do |worker|
Expand Down
1 change: 1 addition & 0 deletions resqutils.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("resque")
s.add_development_dependency("rake")
s.add_development_dependency("rspec")
s.add_development_dependency("fakeredis")
end
35 changes: 35 additions & 0 deletions spec/resque_matchers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'
require 'rspec/version'

describe "Resque RSpec custom matchers" do
include Resqutils::Spec::ResqueHelpers

context "have_job_queued" do
class BackgroundJob
@queue = :low

def self.perform
end
end

class Service
def expensive
Resque.enqueue(BackgroundJob)
end
end

before do
clear_queue(BackgroundJob)
end

it "asserts Resque job has been queued" do
Service.new.expensive

if RSpec::Version::STRING.start_with?("2")
"low".should have_job_queued(class: BackgroundJob, args: [])
else
expect("low").to have_job_queued(class: BackgroundJob, args: [])
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'fakeredis'
require 'resque'
require 'resqutils'
require 'resqutils/spec'

GEM_ROOT = File.expand_path(File.join(File.dirname(__FILE__),'..'))
Dir["#{GEM_ROOT}/spec/support/**/*.rb"].sort.each {|f| require f}
Expand Down
5 changes: 4 additions & 1 deletion spec/stale_workers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
]
}

before do
after do
ENV.delete("RESQUTILS_SECONDS_TO_BE_CONSIDERED_STALE")
end

before do
allow(Resque).to receive(:workers).and_return(workers)
end

Expand Down

0 comments on commit a515d9c

Please sign in to comment.