我试图学习如何在Rails中进行测试。我有一个foods_controller
,在测试文件夹中,我的food.yml
填充了创建新食物对象时应该出现的所有参数,并在foods_controller_test.rb
中,&#中的参数34;应该创造食物"与food.yml
中的匹配。运行测试时出现此错误:
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>
有人可以在这里冒犯我的错误吗?
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
foods_controller.rb
class FoodsController < ApplicationController
before_action :set_food, only: [:show, :edit, :update, :destroy]
before_filter :authorize, only: [:create, :delete]
# GET /foods
# GET /foods.json
def index
@foods = Food.order(:name)
# @foods = @foods.sort_by &:name
# @users.sort! { |a,b| a.name.downcase <=> b.name.downcase }
@food_categories = Food::CATEGORIES.keys.sort
# @current_category ||= params(:category)
day_selected = params[:day_selected]
meal_selected = params[:meal_selected]
end
# GET /foods/1
# GET /foods/1.json
def show
end
# GET /foods/new
def new
@food = Food.new
end
# GET /foods/1/edit
def edit
end
# POST /foods
# POST /foods.json
def create
@food = Food.new(food_params)
respond_to do |format|
if @food.save
format.html { redirect_to foods_url, notice: 'Food was successfully created.' }
format.json { render :show, status: :created, location: @food }
else
format.html { render :new }
format.json { render json: @food.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /foods/1
# PATCH/PUT /foods/1.json
def update
respond_to do |format|
if @food.update(food_params)
format.html { redirect_to foods_url, notice: 'Food was successfully updated.' }
format.json { render :show, status: :ok, location: @food }
else
format.html { render :edit }
format.json { render json: @food.errors, status: :unprocessable_entity }
end
end
end
# DELETE /foods/1
# DELETE /foods/1.json
def destroy
#current_user.entries.where(food_id: "#{@food.id}").delete_all
@food.destroy
respond_to do |format|
format.html { redirect_to foods_url, notice: 'Food was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_food
@food = Food.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def food_params
params.require(:food).permit(:name, :portion, :calories, :fat, :carb, :protein,
:fiber, :sugar, :category, :added_by, :cholesterol,
:potassium, :sodium, :trans_fat, :monounsaturated_fat,
:polyunsaturated_fat, :saturated_fat)
end
end
food.rb
class Food < ActiveRecord::Base
belongs_to :user
CATEGORIES = { "Dairy & Eggs" => "Dairy",
"Meat & Fish" => "Animal",
"Fruits & Vegetables" => "Plant",
"Nuts, beans & legumes" => "Nuts",
"Grains" => "Grains",
"Drinks" => "Beverages",
"Sweets & Candy" => "Sweets",
"Oils & Fats" => "Oils",
"Other" => "Other" }
validates :name, presence: true
validates :portion, presence: true
validates :calories, presence: true
validates :fat, presence: true
validates :carb, presence: true
validates :protein, presence: true
validates :category, presence: true
end
答案 0 :(得分:0)
尝试在您的案例json
或html
中指定测试示例中的格式。
post :create, format: json, food: { ... }