Skip to content

Commit

Permalink
Clean up RSpec matchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerhunt committed Mar 31, 2016
1 parent 05e9101 commit d140709
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
31 changes: 20 additions & 11 deletions spec/support/matchers/be_redirect.rb
@@ -1,13 +1,7 @@
module BeRedirect
class Matcher
attr :expected_status_code
attr :expected_location
attr :actual_status_code
attr :actual_location

def matches?(response)
@actual_status_code, headers, _ = response
@actual_location = headers['Location']
self.actual_status_code, self.actual_headers, _ = response

status_code_matches? && location_matches?
end
Expand Down Expand Up @@ -42,19 +36,34 @@ def failure_message_when_negated
"Did not expect response to #{description}"
end

protected

attr_accessor :actual_headers
attr_accessor :actual_status_code
attr_accessor :expected_location
attr_accessor :expected_status_code

private

LOCATION = 'Location'

def actual_location
actual_headers[LOCATION]
end

def status_code_matches?
if expected_status_code
actual_status_code == expected_status_code
else
actual_status_code.to_s =~ /^30[1237]$/
end
end
private :status_code_matches?

def location_matches?
!expected_location || (expected_location == actual_location)
if expected_location
expected_location == actual_location
end
end
private :location_matches?
end

def be_redirect
Expand All @@ -63,5 +72,5 @@ def be_redirect
end

RSpec.configure do |config|
config.include(BeRedirect)
config.include BeRedirect
end
30 changes: 17 additions & 13 deletions spec/support/matchers/have_header.rb
@@ -1,15 +1,11 @@
module HaveHeader
class Matcher
attr :headers
attr :expected_header
attr :expected_value

def initialize(expected_header)
@expected_header = expected_header
self.expected_header = expected_header
end

def matches?(response)
_, @headers, _ = response
_, self.actual_headers, _ = response

if expected_value
actual_header == expected_value
Expand All @@ -19,18 +15,14 @@ def matches?(response)
end

def with(expected_value)
@expected_value = expected_value
self.expected_value = expected_value
self
end

def actual_header
headers[expected_header]
end

def description
sentence = "have header #{expected_header.inspect}"
sentence << " with #{expected_value.inspect}" if expected_value
sentence << ", got:\n #{headers.inspect}"
sentence
end

def failure_message
Expand All @@ -40,6 +32,18 @@ def failure_message
def failure_message_when_negated
"Did not expect response to #{description}"
end

protected

attr_accessor :actual_headers
attr_accessor :expected_header
attr_accessor :expected_value

private

def actual_header
actual_headers[expected_header]
end
end

def have_header(name)
Expand All @@ -48,5 +52,5 @@ def have_header(name)
end

RSpec.configure do |config|
config.include(HaveHeader)
config.include HaveHeader
end

0 comments on commit d140709

Please sign in to comment.