-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
Mock#to_ary and Array#flatten problem... #31
Comments
@dchelimsky, Have you had a chance to look at this? Even a work around would be nice... Thanks |
Not yet. Not sure when I will. Big backlog and it's the holidays. |
@lardawge Do you have a solution in mind? |
I don't have a solution for fixing rspec-mocks although there is a great suggestion by James Mead in that lighthouse ticket (the direction mocha is headed with a fix). I was able to get the specs for formtastic passing by using an activemodel object which responds correctly to to_ary... |
I have the same problem when testing my controllers that use the authorize method of CanCan which seems to call flatten on an array of mocks models then. |
Stubbing to_ary worked for me as well. I'm not senior enough to propose an overall solution, but couldn't we add Mock#to_ary which just returns nil? |
What about returning |
Returning |
You can't return [self]. flatten proceeds to array contents recursively. Returning [self] creates infinite loop. |
A similar problem just came up with to_hash (#51). I don't think there is a one-size-fits-all solution here for implementing Now we could conceivably solve this for rails-specific problems in the context of rspec-rails, but even then I'm not sure how we can do it in a general way. If anybody has any ideas, please post them to the rspec-rails tracker. |
The problem is that mocked objects don't behave like a regular Object would. When
|
Perhaps we implement it to raise |
I tried implementing
The following failed with the
I then implemented it thusly:
Then the same example passed. This suggests to me that the better choice is to return nil. It solves the I'm going to go ahead and implement it this way, but I'd welcome any additional feedback or concerns about this approach. |
I have gone through the same tests you did before. You can't raise |
So are you saying that method_missing should just delegate to super if the method is to_ary? |
There two things you can do. Throw NoMethodError from |
Returning nil seems simpler and I've already got it working. Do you see any downside? |
Nope. |
Well, one. It's a bit less like a regular Object - which takes the other route. But right now I can't see this causing problems. We can always adjust if it turns out otherwise. |
Actually, the NoMethodError implementation was easy to convert to (I was missing a step before), so let's go with that. I'd rather have it loudly cause a problem then do so quietly. |
I think you need to add the new |
Done. Thx |
No better way to describe this then:
http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/70
Ran into this while trying to run specs for Formtastic in 1.9.2. Problem with just stubbing it is it then breaks other tests because rails uses respond_to?(:to_ary) internally which breaks other tests in formtastic...
Rspec response:
Mock "user" received unexpected message :to_ary with (no args)
# ./lib/formtastic.rb:575:in
flatten' # ./lib/formtastic.rb:575:in
inputs_for_nested_attributes'# ./lib/formtastic.rb:280:in
inputs' # ./spec/inputs_spec.rb:439:in
block (6 levels) in <top (required)>'# ./lib/formtastic.rb:1927:in
block in semantic_form_for' # ./lib/formtastic.rb:1892:in
with_custom_field_error_proc'# ./lib/formtastic.rb:1926:in
semantic_form_for' # ./spec/inputs_spec.rb:438:in
block (5 levels) in <top (required)>'Let me know what else is needed...
The text was updated successfully, but these errors were encountered: