Skip to content

Commit

Permalink
Fix 1.9.2 bug with wrapping doubles in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Mar 23, 2013
1 parent c5a83d4 commit 5416b2c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rspec/mocks/test_double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __initialize_as_test_double(name=nil, stubs_and_options={})
end

def method_missing(message, *args, &block)
raise NoMethodError if message == :to_ary
raise NoMethodError if message == :to_ary || message == :to_a
return 0 if message == :to_int && __mock_proxy.null_object?
__mock_proxy.record_message_received(message, *args, &block)

Expand Down
4 changes: 4 additions & 0 deletions spec/rspec/mocks/mock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def @double.method_with_default_argument(arg={}); end
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it "is wrappable in an array" do
expect( Array(@double) ).to eq([@double])
end

context "with Ruby > 1.8.6", :unless => RUBY_VERSION.to_s == '1.8.6' do
it "passes proc to expectation block without an argument" do
# We eval this because Ruby 1.8.6's syntax parser barfs on { |&block| ... }
Expand Down

9 comments on commit 5416b2c

@exviva
Copy link

@exviva exviva commented on 5416b2c Jul 9, 2013

Choose a reason for hiding this comment

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

I actually want to stub an object, on which I'm calling to_a in the code. Any ideas for workarounds?

@myronmarston
Copy link
Member

Choose a reason for hiding this comment

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

@exviva: can you open an issue showing a code example of what you're trying to do, how you expect it to behave, and what it's doing instead?

@exviva
Copy link

@exviva exviva commented on 5416b2c Jul 9, 2013

Choose a reason for hiding this comment

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

@JonRowe
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI, You can explicitly stub to_a, it's tested all over the place.

@exviva
Copy link

@exviva exviva commented on 5416b2c Jul 10, 2013

Choose a reason for hiding this comment

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

@JonRowe yup, that's what I did, but in my case, the to_a is called in a chain, and I want the null object to return self from to_a, and I can't do anything like d = double(to_a: d).as_null_object :/

@JonRowe
Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately we cannot return self from to_a or to_ary as it breaks Ruby functionality with flatten and Arrays. We could return nil when it's a NullObject.

@exviva
Copy link

@exviva exviva commented on 5416b2c Jul 10, 2013

Choose a reason for hiding this comment

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

I see. So I guess we should close this one?

@JonRowe
Copy link
Member Author

Choose a reason for hiding this comment

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

Well this is a discussion of a commit, you can't close it ;), I'm working on a PR for returning nil when the object is a null object, but it's currently got issues on 1.8.7.

@exviva
Copy link

@exviva exviva commented on 5416b2c Jul 10, 2013

Choose a reason for hiding this comment

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

Yeah, I meant the issue :)

Please sign in to comment.