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

add true option for dependent matcher and test all options #631

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pkg
tags
test/*/log/*.log
tmp
.ruby-version
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this change? Thanks

8 changes: 7 additions & 1 deletion lib/shoulda/matchers/active_record/association_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ module ActiveRecord
# should belong_to(:world).dependent(:destroy)
# end
#
# To test that any :dependent option was specified, pass true.
# # RSpec
# describe Person do
# it { should belong_to(:world).dependent(true) }
# end
#
# ##### counter_cache
#
# Use `counter_cache` to assert that the `:counter_cache` option was
Expand Down Expand Up @@ -993,7 +999,7 @@ def macro_correct?
end

def macro_supports_primary_key?
macro == :belongs_to ||
macro == :belongs_to ||
([:has_many, :has_one].include?(macro) && !through?)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ def description

def matches?(subject)
self.subject = ModelReflector.new(subject, name)

if option_verifier.correct_for_string?(:dependent, dependent)
if correct_for?(dependent)
true
else
self.missing_option = "#{name} should have #{dependent} dependency"
self.missing_option = missing_option_for(name, dependent)
false
end
end
Expand All @@ -34,6 +33,18 @@ def matches?(subject)
def option_verifier
@option_verifier ||= OptionVerifier.new(subject)
end

def correct_for?(dependent=dependent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have default values here? Why not just def correct_for(dependent)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original reason was that there is a dependent method and therefore the argument wouldn't be needed, but I didn't like how the code read if the argument wasn't passed. But, I just made the change that you suggested.

case dependent
when true, false then option_verifier.correct_for_boolean?(:dependent, dependent)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [93/80]

else option_verifier.correct_for_string?(:dependent, dependent)
end
end

def missing_option_for(name=name, dependent=dependent)
"#{name} should have #{dependent == true ? "a" : dependent} dependency"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body end.

end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
require 'unit_spec_helper'

def each_dependent_option(rails_version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three things:

  • This will create a global method and we don't like that :) Can you put this inside of the outmost describe block below, at the bottom? Also, if you make it a class method then you can use it like a macro as you're doing.
  • What about just having this method return the array instead of also iterating through that array? The array itself seems like more of a separate concept to me. That way below it would be dependent_options.each instead of each_dependent_option.
  • I've tried to move away from treating Rails.version (or any version, for that matter) as a string. I would say you can use the rails_version helper provided by UnitTests::RailsVersion but I need to fix one thing about that to make it less cumbersome to make use of, so the way you have it is fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be all set.

case rails_version
when /\A3/
[:destroy, :delete, :nullify, :restrict]
when /\A4/
[:destroy, :delete, :nullify, :restrict_with_exception, :restrict_with_error]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [81/80]

end.each do |option|
yield option
end
end

describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
context 'belong_to' do
it 'accepts a good association with the default foreign key' do
Expand Down Expand Up @@ -645,9 +656,29 @@ def having_many_non_existent_class(model_name, assoc_name, options = {})
expect(Person.new).not_to have_one(:detail)
end

it 'accepts an association with a valid :dependent option' do
expect(having_one_detail(dependent: :destroy)).
to have_one(:detail).dependent(:destroy)
each_dependent_option(Rails.version) do |option|
it 'accepts an association with a valid :dependent option' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

expect(having_one_detail(dependent: option)).
to have_one(:detail).dependent(option)
end
end

it 'accepts any dependent option if true' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

each_dependent_option(Rails.version) do |option|
expect(having_one_detail(dependent: option)).
to have_one(:detail).dependent(true)
end
end

it 'rejects any dependent options if false' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

each_dependent_option(Rails.version) do |option|
expect(having_one_detail(dependent: option)).
to_not have_one(:detail).dependent(false)
end
end

it 'accepts a nil dependent option if false' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

expect(having_one_detail).to have_one(:detail).dependent(false)
end

it 'rejects an association with a bad :dependent option' do
Expand Down