How to find where be_success is defined

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to find where be_success is defined

Ralph Shnelvar
So this is both a specific and a general question

Specific question:
I am trying to find where be_success is defined.  In Ubuntu, I used DoubleCmd (a clone of Window's wonderful Total Commander) to scan for be_success without, uh, success.  That's the specific question.

I can see the following
[73, 82] in /home/real-estate-data-mining/spec/controllers/articles_controller_spec.rb
   
73:       # get :new # , params: {}, session: valid_session
   
74:       byebug
   
75:       get :new, params: {}, session: valid_session
   
76:       # get :new, params: {}, session: {valid_session}
   
77:       byebug
=> 78:       expect(response).to be_success
   
79:     end
   
80:   end
   
81:   end
   
82:
(byebug) response.status
302
(byebug) be_success.class
RSpec::Matchers::BuiltIn::BePredicate
(byebug)


In /home/real-estate-data-mining/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rspec-expectations-3.6.0/lib/rspec/matchers/built_in.rb:178 I see
[240, 249] in /home/real-estate-data-mining/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rspec-expectations-3.6.0/lib/rspec/matchers/built_in/be.rb
   
240:           method_name = actual.respond_to?(predicate) ? predicate : present_tense_predicate
   
241:           @predicate_matches = actual.__send__(method_name, *@args, &@block)
   
242:         end
   
243:
   
244:         def predicate
=> 245:           :"#{@expected}?"
   
246:         end
   
247:
   
248:         def present_tense_predicate
   
249:           :"#{@expected}s?"
(byebug) @expected.class
String
(byebug) @expected
"success"


So to partially answer my own question, one can find where successful? is defined here:

[109, 118] in /home/real-estate-data-mining/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rack-2.0.3/lib/rack/response.rb
   
109:
   
110:     module Helpers
   
111:       def invalid?;             status < 100 || status >= 600;        end
   
112:
   
113:       def informational?;       status >= 100 && status < 200;        end
=> 114:       def successful?;          status >= 200 && status < 300;        end
   
115:       def redirection?;         status >= 300 && status < 400;        end
   
116:       def client_error?;        status >= 400 && status < 500;        end
   
117:       def server_error?;        status >= 500 && status < 600;        end

I'm pretty sure there is a metaprogramming technique for finding where/when an object (in Ruby, a class IS an example of an Object) is created.

So, ... How, in Ruby/Rails/Rspec, would one track where be_success comes into existence?


Bonus question:
Since a Class is an Object, why does
Class.class
return Class ?

Similarly, why does BasicObject.class return Class ?


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/52dbbb13-52b2-48eb-b0d5-d48495e17aca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to find where be_success is defined

Greg Navis
be_* is handled by method_missing in rspec-expectations. When you take a look at BePredicate you can see it tries two variants of the phrase following be_*. For example:

expect(user).to be_registered

is roughly equivalent to:

expect(user.registered?).to be_truthy

Yours

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6WWt8yXou6QN%3D_LASvkRxdickrhj0gbh%3DnEsjvHGYq%2Bc7JRg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...