Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1910 from lime/content-type-matcher-nil
Browse files Browse the repository at this point in the history
Fix a nil error in failure_message of content type validation matcher
  • Loading branch information
tute committed May 24, 2016
2 parents 2c59f96 + cef29db commit d61b706
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ master:

* README adjustments for Ruby beginners (add links, elucidate model in Quick Start)
* Bugfix: Now it's possible to save images from URLs with special characters [#1932]
* Fix a nil error in content type validation matcher [#1910]

5.0.0.beta2 (2015-04-01):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def matches? subject

def failure_message
"#{expected_attachment}\n".tap do |message|
message << accepted_types_and_failures
message << accepted_types_and_failures.to_s
message << "\n\n" if @allowed_types.present? && @rejected_types.present?
message << rejected_types_and_failures
message << rejected_types_and_failures.to_s
end
end

Expand All @@ -55,7 +55,7 @@ def description
def accepted_types_and_failures
if @allowed_types.present?
"Accept content types: #{@allowed_types.join(", ")}\n".tap do |message|
if @missing_allowed_types.any?
if @missing_allowed_types.present?
message << " #{@missing_allowed_types.join(", ")} were rejected."
else
message << " All were accepted successfully."
Expand All @@ -66,7 +66,7 @@ def accepted_types_and_failures
def rejected_types_and_failures
if @rejected_types.present?
"Reject content types: #{@rejected_types.join(", ")}\n".tap do |message|
if @missing_rejected_types.any?
if @missing_rejected_types.present?
message << " #{@missing_rejected_types.join(", ")} were accepted."
else
message << " All were rejected successfully."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,58 @@

it "rejects a class with no validation" do
expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

it 'rejects a class when the validation fails' do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

it "accepts a class with a matching validation" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

it "accepts a class with other validations but matching types" do
Dummy.validates_presence_of :title
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

it "accepts a class that matches and a matcher that only specifies 'allowing'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
matcher = plain_matcher.allowing(%w(image/png image/jpeg))

expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

it "rejects a class that does not match and a matcher that only specifies 'allowing'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
matcher = plain_matcher.allowing(%w(image/png image/jpeg))

expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

it "accepts a class that matches and a matcher that only specifies 'rejecting'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))

expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

it "rejects a class that does not match and a matcher that only specifies 'rejecting'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))

expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end

context "using an :if to control the validation" do
Expand All @@ -75,12 +83,14 @@
dummy = Dummy.new
dummy.go = true
expect(matcher).to accept(dummy)
expect { matcher.failure_message }.to_not raise_error
end

it "does not run the validation if the control is false" do
dummy = Dummy.new
dummy.go = false
expect(matcher).to_not accept(dummy)
expect { matcher.failure_message }.to_not raise_error
end
end

Expand Down

0 comments on commit d61b706

Please sign in to comment.