-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding json responses in rooms_controller. Refs issue 143.
- Loading branch information
Showing
3 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
spec/support/matchers/shoulda/respond_with_json_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module Shoulda # :nodoc: | ||
module Matchers | ||
module ActionController # :nodoc: | ||
|
||
# Ensures a controller responded with the expected json. | ||
def respond_with_json(status) | ||
RespondWithJsonMatcher.new(status) | ||
end | ||
|
||
class RespondWithJsonMatcher # :nodoc: | ||
|
||
def initialize(expected_json) | ||
@no_values = false | ||
@ignored = [] | ||
@expected = expected_json | ||
end | ||
|
||
def ignoring_attributes(ignored = ['id', 'updated_at', 'created_at']) | ||
@ignored = ignored | ||
self | ||
end | ||
|
||
def ignoring_values() | ||
@no_values = true | ||
self | ||
end | ||
|
||
def matches?(controller) | ||
@actual = controller.response.body | ||
|
||
# remove the selected attributes | ||
@ignored.each do |attr| | ||
@actual.gsub!(/.#{attr}.:[^{,]*[,]+/, "") | ||
@expected.gsub!(/.#{attr}.:[^{,]*[,]+/, "") | ||
end | ||
# remove the values of all attributes | ||
if @no_values | ||
@actual.gsub!(/:[^{,]*/, "") | ||
@expected.gsub!(/:[^{,]*/, "") | ||
end | ||
|
||
@actual == @expected | ||
end | ||
|
||
def failure_message | ||
s = "Expected the following json strings to be equal: \n" | ||
s += "Expected: " + @expected.inspect + "\n" | ||
s += " Got: " + @actual.inspect | ||
s | ||
end | ||
|
||
def negative_failure_message | ||
s = "Expected the following json strings to be different: \n" | ||
s += "Expected: " + @expected.inspect + "\n" | ||
s += " Got: " + @actual.inspect | ||
s | ||
end | ||
|
||
def description | ||
"respond with a defined json string" | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |