2016-12-03 37 views
0

我正試圖學習如何在Rails中進行測試。我有一個foods_controller,在測試文件夾中,我的food.yml填充了創建新食物時應該存在的所有參數,而在foods_controller_test.rb中,「應該創建食物」中的參數與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> 

任何人都可以exaplain我這裏有什麼錯嗎?

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 
+1

你好,你有一個before_filter授權在你的控制器。所以在你的測試用例中,你必須在創建之前進行授權。你在使用設計嗎?然後嘗試使用設計助手進行測試。這將解決您的問題。 –

回答

0

嘗試在測試示例中指定格式,在您的示例中爲jsonhtml

post :create, format: json, food: { ... }