2016-04-25 81 views
1

Comments_controller_test.rbMINITEST的帖子在控制器

require 'test_helper' 

class CommentsControllerTest < ActionController::TestCase 
    test "should create a comment" do 

    assert_difference('Comment.count') do 
     post :create, comment: {user_id: 1, job_id: 1, content: "This is a comment"} 
    end 

    end 
end 

Comments_controller.rb

class CommentsController < ApplicationController 

def create 
    @comment = Comment.new(comment_params) 

    if @comment.save 
     render :json => {:status => 'success', :entry => @comment} 
    else 
     render :json => {:status => 'error'} 
    end 
end 

    def comment_params 
    params.require(:comment).permit(:job_id, :user_id, :content) 
    end 
end 

評論表:

class CreateComments < ActiveRecord::Migration 
    def change 
    create_table :comments do |t| 
     t.string :user_id 
     t.string :job_id 
     t.string :content 

     t.timestamps null: false 
    end 
    end 
end 

當我跑,我得到:

Finished in 0.162164s, 110.9987 runs/s, 117.1653 assertions/s. 

1) Failure: 
CommentsControllerTest#test_should_create_a_comment  

"Comment.count" didn't change by 1. 
Expected: 4 
Actual: 3 

任何想法,爲什麼這沒有被創建?創建註釋適用於實際的應用程序,但由於某種原因,它沒有通過測試

注意:我在夾具中創建了3條註釋(這就是它得到3的計數)。

+0

完美!你是對的。它說我正在重定向,因爲我沒有登錄。我不得不把它放在我的comment_controller_test.rb文件中:setup do controller.class.skip_before_filter:require_login end – user3007294

回答

2

您可以調試來自服務器的響應。 在post :create, comment: { ... }後面添加puts response.body並檢查日誌。

相關問題