这是在 rspec 控制器测试中导致模板错误的部分形式:
<%= f.label :expense_for, '栏目:'%>
<%= f.select :expense_for, @expense_for.map { |expense| [expense, expense] }, {:include_blank => true, :selected => @expense.expense_for } %>
我们在费用控制器测试中使用 render_views。由于 @expense_for 在创建测试中为零,因此 .map 会导致错误:
[31mActionView::Template::Error:←[0m
←[31mundefined method `map' for nil:NilClass
导致上述错误的 rspec 代码是:
get 'create', :expense => e, :expense_for => ['a','b']
在费用控制器中,@expense_for 在 new 中定义为:
def new
if has_create_right?
@expense = Expense.new(params[:expense], :as => :role_new)
if @expense.expense_type == 'production'
@expense_for = return_expense_for_production
elsif @expense.expense_type == 'overhead'
@expense_for = return_expense_for_overhead
else
@expense_for = []
end
end
@expense_for 充满了 ajax 调用。
如何以部分形式将值传递给 @expense_for 以进行 rspec 测试?谢谢。