Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation warning for uniqueness matcher (Rails 4.2) #1073

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ def update_existing_record!(value)
end

def arbitrary_non_blank_value
non_blank_value = dummy_value_for(@attribute)
limit = column_limit_for(@attribute)
non_blank_value = 'an arbitrary value'

if limit && limit < non_blank_value.length
'x' * limit
Expand Down
34 changes: 21 additions & 13 deletions spec/support/unit/matchers/print_warning_including.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,51 @@ def initialize(expected_warning)

def matches?(block)
@captured_stderr = collapse_whitespace(capture(:stderr, &block))
@was_negated = false
captured_stderr.include?(expected_warning)
end

def does_not_match?(block)
!matches?(block).tap do
@was_negated = true
end
end

def failure_message
"Expected block to #{expectation}\nbut actually printed#{actual_warning}"
"Expected block to #{expectation}\n\nHowever, #{aberration}"
end
alias_method :failure_message_for_should, :failure_message

def failure_message_when_negated
"Expected block not to #{expectation}, but it did."
"Expected block not to #{expectation}\n\nHowever, #{aberration}"
end
alias_method :failure_message_for_should_not,
:failure_message_when_negated

def description
"should #{expectation}"
"should print a warning containing #{expected_warning.inspect}"
end

def supports_block_expectations?
true
end

protected
private

attr_reader :expected_warning, :captured_stderr

private
def was_negated?
@was_negated
end

def expectation
"print a warning including:\n #{expected_warning}"
"print a warning containing:\n\n #{expected_warning}"
end

def actual_warning
if captured_stderr.empty?
" nothing."
def aberration
if was_negated?
'it did.'
elsif captured_stderr.empty?
'it actually printed nothing.'
else
":\n #{captured_stderr}"
"it actually printed:\n\n #{captured_stderr}"
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,11 @@

message = <<-MESSAGE
Example did not properly validate that :attr is case-sensitively unique.
After taking the given Example, setting its :attr to ‹"an arbitrary
value"›, and saving it as the existing record, then making a new
Example and setting its :attr to a different value, ‹"AN ARBITRARY
VALUE"›, the matcher expected the new Example to be valid, but it was
invalid instead, producing these validation errors:
After taking the given Example, setting its :attr to ‹"dummy value"›,
and saving it as the existing record, then making a new Example and
setting its :attr to a different value, ‹"DUMMY VALUE"›, the matcher
expected the new Example to be valid, but it was invalid instead,
producing these validation errors:

* attr: ["has already been taken"]
MESSAGE
Expand Down Expand Up @@ -1355,12 +1355,12 @@ def name=(name)

message = <<-MESSAGE.strip
Example did not properly validate that :name is case-sensitively unique.
After taking the given Example, setting its :name to ‹"an arbitrary
value"› (read back as ‹"AN ARBITRARY VALUE"›), and saving it as the
existing record, then making a new Example and setting its :name to
‹"an arbitrary value"› (read back as ‹"AN ARBITRARY VALUE"›) as well,
the matcher expected the new Example to be valid, but it was invalid
instead, producing these validation errors:
After taking the given Example, setting its :name to ‹"dummy value"›
(read back as ‹"DUMMY VALUE"›), and saving it as the existing record,
then making a new Example and setting its :name to ‹"dummy value"›
(read back as ‹"DUMMY VALUE"›) as well, the matcher expected the new
Example to be valid, but it was invalid instead, producing these
validation errors:

* name: ["has already been taken"]

Expand Down Expand Up @@ -1419,6 +1419,18 @@ def name=(name)
end
end

context 'when the column is a boolean column' do
it 'accepts (and does not print a warning)' do
record = build_record_validating_uniqueness(attribute_type: :boolean)
running_validation = -> { expect(record).to validate_uniqueness }
message =
'You attempted to assign a value which is not explicitly `true` or ' +
'`false`'

expect(&running_validation).not_to print_warning_including(message)
end
end

let(:model_attributes) { {} }

def default_attribute
Expand Down