Commit Graph

5 Commits

Author SHA1 Message Date
Elliot Winkler 0d43835b61 Fix #permit so #on works and is properly tested
Why:

* When using #permit with the #on qualifier to assert that #permit was
  called on a subset of the params hash (selected using #require), the
  matcher would fail. The matcher didn't properly detect that #permit
  was called on the slice -- it thought it was called on the parent
  params object.
* It turns out this was a bug in Doublespeak. When #require is called,
  we take the subset of the params, which is also an
  ActionController::Parameters object like params, and we stub #permit
  on it at runtime. Unfortunately the Double object created here was
  never activated, so when #permit was called, this Double wasn't the
  one run. Instead, the Double on #permit within the
  ActionController::Parameters class (which is stubbed at the beginning)
  was the one that was run, and it's this object that recorded the call
  on #permit incorrectly.
* This bug slipped through because it turns out when #on was added it
  wasn't tested very well.

To satisfy the above:

* Modify Doublespeak so that when it is in activated mode, whenever
  new doubles are created, activate them immediately.
* Fix all of the tests for #permit around operating on a slice of the
  params hash so that they use the #on qualifier.
2015-09-29 10:29:46 -06:00
Elliot Winkler 755b3142a5 Doublespeak: Only store original method once
If a method for a class is doubled more than once within the same test
run, the original implementation of that method will change from double
to double, even if the double is deactivated correctly. Say we have two
distinct tests that both double the same method. Here is how that method
will be overridden as it goes along:

    A) START: original method
    B) ACTIVATE (1st test): method is doubled
    C) DEACTIVATE (1st test): calls original method (A)
    D) ACTIVATE (2nd test): original method (C) stored; method is again
                            doubled
    E) DEACTIVATE (2nd test): calls original method (C)

With this commit, this changes to:

    A) START: original method
    B) ACTIVATE (1st test): method is doubled
    C) DEACTIVATE (1st test): calls original method (A)
    D) ACTIVATE (2nd test): original method not stored again; method is
                            again doubled
    E) DEACTIVATE (2nd test): calls original method (A)
2015-03-01 00:39:51 -07:00
Doug Orleans f81add5117 Allow multiple StrongParametersMatchers to exist at once.
The provided RSpec example looks a bit contrived, but it corresponds to
this perfectly reasonable Minitest test case that was not working:

  class UserControllerTest < ActionController::TestCase
    should permit(:name).for(:create)
    should_not permit(:admin).for(:create)
  end

This instantiates two matchers at class load time, which resulted in the
second one overriding the double collection of the first one. Then when
the tests are executed, the first matcher would fail, because its double
is not listening to ActionController::Parameters#permit -- only the
second matcher's double collection gets activated.

This is fixed in Doublespeak by only creating one double collection per
class.
2014-06-21 21:56:47 -06:00
Elliot Winkler c22d7c89e0 Extract examples in README to inline documentation 2014-06-20 16:41:27 -06:00
Elliot Winkler dfebd81af0 Add a small stubbing library
This provides a robust solution for temporarily stubbing (and
unstubbing) methods. It will be internally by the strong parameters and
delegation matchers.
2014-04-22 09:37:27 -05:00