我正在通过railstutorial.org学习Rails,但我对一些事情感到困惑:
在本章 http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two#sec:password_migration作者告诉我们在控制台中进行一些测试respond_to?
User 对象上的方法,它工作正常。但后来,当我们为:encrypted_password
属性,他使用respond_to
.
出于好奇,我尝试了respond_to
在控制台中,对于 User 对象,我收到一条错误消息,指出该方法不存在。唉,如果我尝试使用编写测试respond_to?
代替respond_to
,测试不运行。
有人可以向我解释一下区别吗?为什么测试只运行respond_to
?
红宝石款待?
and !
作为方法名称中的实际字符。respond_to
and respond_to?
是不同的。?
表明这should回答正确或错误(按照惯例;这不是必需的)。具体来说:
respond_to?
is a Ruby method http://ruby-doc.org/core/Object.html#method-i-respond_to-3F用于检测该类是否具有特定方法。例如,
@user.respond_to?('eat_food')
将返回 true 如果User
类有一个eat_food
方法就可以了。
respond_to
is a Rails method http://apidock.com/rails/ActionController/MimeResponds/respond_to用于响应特定的请求类型。例如:
def index
@people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => @people.to_xml }
end
end
但是,在您提供的 RailsTutorial 链接中,您会看到RSpec method should
与 RSpec 交互respond_to http://rubydoc.info/gems/rspec-expectations/2.4.0/RSpec/Matchers#respond_to-instance_method方法。除非您运行,否则这在您的控制台中不可用rails console test
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)