samedi 3 décembre 2016

ArgumentError too few arguments when trying to test the controller

I'm trying to learn how to do tests in Rails. I have a foods_controller and in the test folder my food.yml is filled with all of the parameters that should be present when creating a new food object and in foods_controller_test.rb the parameters in "should create food" are matching the ones in food.yml. When running a test I get this error:

ArgumentError: too few arguments
    app/controllers/application_controller.rb:45:in `format'
    app/controllers/application_controller.rb:45:in `authorize'
    test/controllers/foods_controller_test.rb:21:in `block (2 levels) in <class:FoodsControllerTest>'
    test/controllers/foods_controller_test.rb:20:in `block in <class:FoodsControllerTest>

Can anyone exaplain me what is wrong here?

food.yml

one:
  name: "Whatever"
  portion: "100g"
  calories: 1
  fat: 1.5
  carb: 1.5
  protein: 1.5
  fiber: 1.5
  sugar: 1.5
  category: "Grains"

two:
  name: "MyString"
  portion: "MyString"
  calories: 1
  fat: 1.5
  carb: 1.5
  protein: 1.5
  fiber: 1.5
  sugar: 1.5
  category: "MyString"

foods_controller_test.rb

require 'test_helper'

class FoodsControllerTest < ActionController::TestCase
  setup do
    @food = foods(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:foods)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create food" do
    assert_difference('Food.count') do
      post :create, food: { calories: @food.calories, carb: @food.carb, category: @food.category, fat: @food.fat, fiber: @food.fiber, name: @food.name, portion: @food.portion, protein: @food.protein, sugar: @food.sugar }
    end

    assert_redirected_to food_path(assigns(:food))
  end

  test "should show food" do
    get :show, id: @food
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @food
    assert_response :success
  end

  test "should update food" do
    patch :update, id: @food, food: { calories: @food.calories, carb: @food.carb, category: @food.category, fat: @food.fat, fiber: @food.fiber, name: @food.name, portion: @food.portion, protein: @food.protein, sugar: @food.sugar }
    assert_redirected_to food_path(assigns(:food))
  end

  test "should destroy food" do
    assert_difference('Food.count', -1) do
      delete :destroy, id: @food
    end

    assert_redirected_to foods_path
  end
end

Aucun commentaire:

Enregistrer un commentaire