user938363
user938363

Reputation: 10368

How to pass a value for variable in view in rspec controller test for rails 3.2.8 app?

Here is the partial form causing the Template error in rspec controller test:

<%= f.label :expense_for, '栏目:'%>
<%= f.select :expense_for, @expense_for.map { |expense| [expense, expense] }, {:include_blank => true, :selected => @expense.expense_for } %>

We use render_views in expenses controller test. Since @expense_for is nil in create test, so .map causes error:

[31mActionView::Template::Error:←[0m
       ←[31mundefined method `map' for nil:NilClass

The rspec code which causes the error above is:

get 'create', :expense => e, :expense_for => ['a','b']

In expenses controller, @expense_for is defined in new as:

  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

The @expense_for is filled up with ajax call.

How can I pass a value to @expense_for in partial form for rspec test? Thanks.

Upvotes: 0

Views: 332

Answers (1)

x1a4
x1a4

Reputation: 19485

if @expense_for is nil in your test, then has_create_right? is false, so @expense_for is never being set. Try setting @expense_for to [] at the beginning of the action, instead of in the else clause.

Upvotes: 1

Related Questions