Rails控制器测试错误

时间:2015-09-22 08:56:18

标签: ruby-on-rails testing

我正在进行控制器测试,但是我有这个错误。我的模型测试通过,但关于这一点,我不知道发生了什么:

/cook-book/test/controllers/recipes_controller_test.rb:23]:  "Recipe.count" didn't change by 1 Expected: 3 Actual: 2

我的'recipes_controller_test.rb':

require 'test_helper'

class RecipesControllerTest < ActionController::TestCase
  include Devise::TestHelpers
    setup do
       @recipe = recipes(:one)
       user = User.create!(email: "example2@test.com", password:      "password",password_confirmation: "password" )
       sign_in user
    end  
   ....
  test "should create recipe" do
    assert_difference('Recipe.count') do
      post :create, recipe: { title: @recipe.title, image_file_name: @recipe.image_file_name, ingredients: @recipe.ingredients, description: @recipe.description } 
      end

    assert_redirected_to recipe_path(assigns(:recipe))
   end 

'recipes.yml':

one:
 title: MyString
 image_file_name: Myimage
 ingredients: MyText
 description: MyText

two:
 title: MyString
 image_file_name: Myimage
 ingredients: MyText
 description: MyText

和食谱模型:

class Recipe < ActiveRecord::Base
  belongs_to :category
  belongs_to :user
  has_many :comments
  has_attached_file :image, styles: { medium: "400x400#", small: "250x250#"}
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
  validates :title, presence: true, length: { maximum: 30 }
  validates :ingredients, presence: true
  validates :description, presence: true
  validates :image, presence: true
  searchkick
end

@Edit

我的'recipe_controller'

Class RecipesController < ApplicationController
before_action :find_recipe, only: [ :show, :edit, :update, :destroy ]
before_action :authenticate_user!, except: [:index, :show]
.....

def create
    @recipe = current_user.recipes.build(recipe_params)

    if @recipe.save
        redirect_to @recipe
    else
        render 'new'
    end
end
private

def find_recipe
    @recipe = Recipe.find(params[:id])
end

def recipe_params
    params.require(:recipe).permit(:title, :ingredients, :description, :image, :category_id)
end

谢谢!

0 个答案:

没有答案