Commit Graph

31 Commits

Author SHA1 Message Date
Chulki Lee f835cf439e Reject associations when class is not defined 2014-01-17 16:41:43 -07:00
Elliot Winkler fcbb5c485e Convert to 1.9 hash syntax 2014-01-17 13:20:44 -07:00
Christopher Chow 1878e89a13 Update deprecated matcher protocol for RSpec 3.
The methods failure_message_for_should and failure_message_for_should_not
have been updated to failure_message and failure_message_negated respectively.
Alias to the old methods to remain backwards compatibility with RSpec 2.
2013-12-24 22:34:26 +11:00
Micah Frost d5e1a94aa2 Use ActiveSupport delegation over Ruby Forwardable
The delegation style found in the AssociationMatcher
is an ActiveSupport style; requiring `forwardable`
without extending it does not add delegation support.
2013-12-13 11:34:53 -07:00
Luciano Sousa 803c06b0b0 Association matchers now support :source option 2013-12-01 15:12:49 -05:00
Elliot Winkler 2b68f3859b Use relations to define conditions and order on associations
In Rails 4, the following construct:

  has_many :children, conditions: { adopted: true }

changes to:

  has_many :children, lambda { where(adopted: true) }

As a result, the way we check the conditions attached to a has_many
changes too: instead of accessing `reflection.options`, we have to use
`reflection.scope` -- this which refers to the lambda above, so we have
to evaluate it and then grab the `where` from the Relation that the
lambda returns.
2013-08-16 16:17:22 -06:00
Elliot Winkler 55f45d9549 Refactor AssociationMatcher to use new OptionVerifier
When using an association matcher you may have qualifiers on that
matcher which let you make assertions on options passed to the
association method that you are testing. For instance, has_many has a
:dependent option and so in order to test this you say something like

    it { should have_many(:people).dependent(:destroy) }

In order to test such an option we have to compare the option you passed
with what the actual value of that option is. This is usually obtained
by looking at the reflection object of the association in question,
although it can be obtained by other means too.

Anyway, the code that does this comparison isn't terrible, but there are
two problems with it. First, it involves typecasting both expected and
actual values. For instance, this:

    has_many :people, dependent: :destroy
    it { should have_many(:people).dependent(:destroy) }

should be equivalent to:

    has_many :people, dependent: :destroy
    it { should have_many(:people).dependent('destroy') }

should be equivalent to:

    has_many :people, dependent: 'destroy'
    it { should have_many(:people).dependent(:destroy) }

Second, we are a little screwed if the method of obtaining the actual
value of the option changes depending on which Rails version you're
using.

So, OptionVerifier attempts to address both of these issues. It's a
little crazy, but it works.

I also moved some methods from AssociationMatcher to ModelReflector
where they really belong.
2013-08-16 16:00:17 -06:00
Fabio Rehm 3f7c440851 Add support for counter_cache option in belongs_to associations 2013-06-21 10:53:30 -04:00
Melissa Xie 718c8b6dd0 Replace instance variables with readable attributes in AssociationMatcher 2013-06-14 13:45:13 -04:00
Melissa Xie a8e2c5145d Swap out protected for private methods in AssociationMatcher
This also includes some method rearranging so that each would fall under the
correct scope.
2013-06-14 13:45:12 -04:00
Melissa Xie 964dcfe655 Extract submatchers from AssociationMatcher
Refactored AssociationMatcher so that `#order`, `#through`, and `#dependent`
would be their own submatchers. This reduces some of the clutter in the main class,
especially as we continue expanding it. In addition, a few related tests were
modified so that they would check failure messages also.
2013-06-14 13:44:37 -04:00
Peter Haza 2b9130b462 Fix have_and_belong_to_many assertion in Rails 4 2013-05-10 10:08:05 -04:00
Panupan Sriautharawong 5ad04a89e2 Add touch option to belongs_to matcher.
* Also fixes validate failing when specified on model but not on matcher.
2013-04-26 09:24:48 -04:00
Chris Bandy d5af9bb8c0 Change class_name to assert on the real class name 2013-03-25 15:49:21 -04:00
Brian Shaver 4f8684c6db Rename failure_message and negative_failure_message
* fixes #108 : RSpec 2.10 compatibility
* remove unnecessary attr_readr
2013-03-08 14:49:12 -05:00
Gabe Berke-Williams af8f9a7bff Use current thoughtbot style in specs 2012-12-26 22:45:54 -05:00
Jacob Morris ca8a1e5a8b allow testing of :foreign_key option for has_one relationships 2012-09-21 13:43:23 -04:00
Karle Durante 41c763d276 Using ActiveRecord::Base.connection is presumptuous. You must infer the home of the join table using the parent model. This allows developers to define HABTM relationships in other databases and still use the 'have_and_belong_to_many' matcher. 2012-09-21 11:40:39 -04:00
Daniel Tamiosso e831348a05 Add a #validate check for association matchers 2012-07-09 14:51:45 +02:00
Gabe Berke-Williams a718c46814 WIP: start to move to using an options hash. 2012-04-24 16:21:23 -05:00
Gabe Berke-Williams 5baa056f35 Bump rspec-rails. 2012-04-20 14:15:56 -04:00
Gabe Berke-Williams 3b3181b11a Whitespace. 2012-03-25 21:49:36 -04:00
Gabe Berke-Williams 41bccc8cbf Cleanup. 2012-03-23 19:50:08 -04:00
Mike Bloy 5c173bf1b7 test and bugfix for failing case 2012-03-22 21:28:57 -05:00
Mike Bloy c863f7e6f0 association_matcher foreign_key refinement
https://github.com/thoughtbot/shoulda-matchers/issues/66

has_many and has_one tests failed with the reverse association has a
nonstandard name, (eg "belongs_to :author, :class_name => :User)

corrected by having have_one and have_many association tests check for an
:inverse_of on their association, and use the inverse relationship's
foreign key info if it can be found
2012-03-22 21:27:21 -05:00
Markus Schwed 17ac7d5a36 Added class_name option matcher to association_matcher 2011-10-16 14:06:24 +02:00
Håkon Lerring 0820901e4b Added support for testing the :conditions option on associations 2011-09-23 13:19:17 +02:00
Prem Sichanugrist a6fa83e18c Fix whitespace 2011-09-12 17:25:05 +07:00
Oscar Del Ben 7e999d73a6 add order clause 2011-06-16 16:21:57 +02:00
Joel Moss 6b4035c117 Prefer #foreign_key to #primary_key_name when possible for AssociationMatcher#foreign_key
Fixes deprecation warning added in Rails 3.1:
https://github.com/rails/rails/blob/v3.1.0.beta1/activerecord/lib/active_record/reflection.rb#L201-208

Closes: https://github.com/thoughtbot/shoulda-matchers/pull/9
2011-05-20 10:22:08 -04:00
Joe Ferris 56b0a0439e Move into the Matchers namespace 2010-12-15 17:34:19 -05:00