Rails开发中的规范测试:从视图到控制器
1. 指定辅助方法
在Rails开发中,辅助方法(Helpers)能将模型转换、标记生成和其他视图逻辑与ERB模板清晰分离,使模板更简洁、易维护,也便于复用那些在应用中反复出现的显示片段。
例如,要实现仅向管理员显示视图部分的常见需求,可以使用块辅助方法:
<%- display_for(:admin) do -%>
Only admins should see this
<%- end -%>
rspec-rails 插件提供了专门的 ExampleGroup 来单独指定辅助方法。以下是一个示例,假设视图可以访问 current_user() 方法,当当前用户具有指定角色时的情况:
# spec/helpers/application_helper_spec.rb
require 'spec_helper'
describe ApplicationHelper do
describe "#display_for(:role)" do
context "when the current user has the role" do
it "displays the content" do
user = stub('User', :in_role? => true)
helper.stub(:
超级会员免费看
订阅专栏 解锁全文
27

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



