I am learning Rails with railstutorial.org, and I am confused about something:
in this chapter the author tells us to do some testing in the console with the respond_to? method on a User object, and it works ok. But later, when we write the test for the :encrypted_password attribute, he uses respond_to.
Out of curiosity, I tried respond_to in the console, for a User object, and I get an error saying the method doesnt exist. Alas, if I try to write the test using respond_to? instead of respond_to, the test doesnt run.
Could someone explain me the difference, and why does the test only run with respond_to?
Ruby treats
?and!as actual characters in a method name.respond_toandrespond_to?are different.?indicates that this should respond with a true or false (by convention; this is not a requirement). Specifically:respond_to?is a Ruby method for detecting whether the class has a particular method on it. For example,would return true if the
Userclass has aneat_foodmethod on it.respond_tois a Rails method for responding to particular request types. For example:However, in the RailsTutorial link you’ve provided, you’re seeing an RSpec method
shouldinteracting with RSpec’srespond_tomethod. This wouldn’t be available in your console, unless you runrails console test.