Rails 控制器与模型的规范编写
1. 控制器模板渲染与重定向
在控制器测试中,有多种方式来指定渲染的模板和重定向的位置。
- 渲染模板 :
- 第一种方式是传入完整的模板路径,如在 MessagesController 中渲染 messages/new 模板。
- 第二种是简写形式,如果渲染的模板是当前测试控制器的一部分,只需传入模板名,例如:
# this will expand to "messages/new" in a MessagesController spec
response.should render_template("new")
- 第三种是指定包含文件名扩展名的完整模板文件名,这样可以让控制器以应用运行时的方式选择模板。例如,当请求 JavaScript 时,期望控制器找到并渲染 `messages/new.js.erb` 模板:
# controller action
def new
respond_to :js, :html
end
# in the spec
get :new, :format => "js"
response.should render_template("new.js.erb")
- 重定向 :使用
redir
超级会员免费看
订阅专栏 解锁全文
61

被折叠的 条评论
为什么被折叠?



