TopperH
TopperH

Reputation: 2223

Rspec Capybara ActionView::Template::Error: undefined method `action' for nil:NilClass

I'm feature testing the edit view of a model called Orders.

Testing the page as a regular user works fine and the test passes:

scenario 'a registered user can actually edit the comment of his order' do
    mycart = create(:cart)
    vest = create(:product, name: 'vest', price: '250000')
    jacket = create(:product, name: 'jacket-1', price: '300000')
    line_vest = create(:line_item, product: vest, unit_price: vest.price, cart: mycart)
    line_jacket = create(:line_item, product: jacket, unit_price: jacket.price, cart: mycart)
    Capybara.current_session.driver.header 'Referer', root_path
    order = create(:order, cart: mycart, user_id: 5)
    user = FactoryGirl.create(:user, id: 5)
    login_as(user)
    visit edit_order_path(order)
    fill_in 'Comment', with: 'This is the best order I have ever done'
    click_button 'Update Order'
    expect(page).to have_content 'This is the best order I have ever done'
  end

If I try to run the same test as an admin, and edit the comment on a order that belongs to another user the test fails:

scenario 'an admin can edit the comment of any order' do
    mycart = create(:cart)
    vest = create(:product, name: 'vest', price: '250000')
    jacket = create(:product, name: 'jacket-1', price: '300000')
    line_vest = create(:line_item, product: vest, unit_price: vest.price, cart: mycart)
    line_jacket = create(:line_item, product: jacket, unit_price: jacket.price, cart: mycart)
    Capybara.current_session.driver.header 'Referer', root_path
    order = create(:order, cart: mycart, user_id: 5)
    admin = FactoryGirl.create(:user, :admin)
    login_as(admin)
    visit edit_order_path(order)
    fill_in 'Comment', with: 'This is the best order I have ever done'
    click_button 'Update Order'
    expect(page).to have_content 'This is the best order I have ever done'
  end

And I get this error:

Failure/Error: click_button 'Update Order' 
ActionView::Template::Error:
undefined method `action' for nil:NilClass 

Anyway if I run the webserver and, as an administrator try to update an order (belonging to any user) it works just fine, so it's just the test not working.

Here are the relevant controller lines:

  before_action :set_order, only: [:show, :update, :edit, :destroy]

 def edit
    unless (@order.user_id == current_user.id) or current_user.admin?
      redirect_to :back, :alert => "You are not authorized to view this page."
    end
  end

  def update
    flash[:notice] = 'Order was successfully updated.' if @order.update(order_params)
    respond_with(@order)
  end

    def set_order
      @order = Order.find(params[:id])
    end

Upvotes: 0

Views: 591

Answers (1)

Theo Chirica
Theo Chirica

Reputation: 4516

I got some similar errors like this.

If you get an error like:

undefined method "method_name" for nil:NilClass

Short explanation is that your database does not have something that your view or controller need to instantiate an object. For example if you have @currency.get_symbol, you need to create a fixture or factory to insert a currency into database for your tests to pass.

Long explanation: you can read this article for a more advanced explanation and solution.

Upvotes: 1

Related Questions