How to find where be_success is defined

29 views
Skip to first unread message

Ralph Shnelvar

unread,
Jul 15, 2017, 1:06:16 AM7/15/17
to Ruby on Rails: Talk
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 ?


Greg Navis

unread,
Jul 15, 2017, 3:51:46 AM7/15/17
to rubyonra...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages