2013-06-09 1 views
0

저는 레일스에 관해서 매우 익숙하며, 루비에 대한 지식은 꽤 녹슨합니다. 어쨌든, 필자는 첫 번째 레일즈 애플리케이션을 올바른 테스트 커버리지로 올바르게 작성하려고 노력하고있다. 나는 getting started 가이드를 따라이를 수행하기 위해 testing 가이드와 결합하려고 노력했습니다.레일에있는 게시물에 속한 주석의 컨트롤러를 테스트하십시오.

그래서, 지금은 Comments 컨트롤러 방법을 추가 테스트 노력에 붙어있어 (왜 함께하지 이러한 두 가지!? 있었다) : 내가 함께있어 늘어나는만큼

class CommentsController < ApplicationController 
    def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.create(params[:comment]) 
    redirect_to post_path(@post) 
    end 
end 

이것은 내 테스트 :

one: 
    commenter: mystring 
    body: mytext 
    post: 

two: 
    commenter: mystring 
    body: mytext 
    post: 

내 문제는 내가 CRE하는 방법을 볼 수 없습니다입니다이기구와

class CommentsControllerTest < ActionController::TestCase 
    setup do 
    @comment = comments(:one) 
    end 
    test "should create comment" do 
    assert_difference('Comment.count') do 
     post :create, comment: { body: @comment.body, commenter: @comment.commenter, ???? } 
    end 

    assert_redirected_to post_path(assigns(:post)) #???? 
    end 
end 

관용적 인 레일스 방식으로 댓글을 게시하고 부모의 의견을 게시 할 수 있습니다.

어떻게하면됩니까?

답변

1

컨트롤러에 post_id 매개 변수를 추가해야합니다. 예 :

class CommentsControllerTest < ActionController::TestCase 

    setup do 
    @comment = comments(:one) 
    @post = posts(:post_one) 
    end 

    test "should create comment" do 
    assert_difference('Comment.count') do 
     post :create, comment: { body: @comment.body, commenter: @comment.commenter }, post_id: @post.id 
    end  
    assert_redirected_to post_path(assigns(:post)) 
    end 

end 

이 코드는 당신이 당신의 설비에 :post_one에 의해 식별되는 Post을 정의했다고 가정합니다.

관련 문제