如何在rails 3.2.8 app的rspec控制器测试中传递视图中的变量值?

时间:2012-08-24 00:24:13

标签: ruby-on-rails ruby-on-rails-3 rspec2

这是在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测试?谢谢。

1 个答案:

答案 0 :(得分:1)

如果您的测试中@expense_for为零,则has_create_right?为false,因此永远不会设置@expense_for。尝试在操作开头设置@expense_for[],而不是在else子句中设置。