2012-07-29 120 views
1

我有一個功能測試,保持失敗,我不知道爲什麼。這是論壇的一部分,測試的目的是確保帖子的作者被允許刪除他們自己的帖子。導軌功能測試不會破壞

我能夠在控制檯和瀏覽器中銷燬帖子,當我手動嘗試時,我無法弄清楚發生了什麼問題。

這裏是控制器的破壞作用:

def destroy 
    @post = Post.find(params[:id]) 
    if @post.player_id == current_player || current_player.admin == true # I can't delete anyone else's posts unless I am the administrator. 
if @post.topic.posts_count > 1 # if topic is more than one post, delete just the post 
    @post.destroy 
    flash[:notice] = "Post was successfully destroyed." 
    redirect_to topic_path(@post.topic) 
else # else, if the topic is only one post, delete the whole thing 
    @post.topic.destroy 
    flash[:notice] = "Topic was successfully deleted." 
    redirect_to forum_path(@post.forum) 
    end 
    else # You are not the admin or the topic starter 
    flash[:notice] = "You do not have rights to delete this post." 
    redirect_to topic_path(@post.topic) 
    end 
end 

這裏是posts.yml文件:

one: 
    id: 1 
    body: MyText 
    forum_id: 1 
    topic_id: 1 
    player_id: 2 
two: 
    id: 2 
    body: MyText 
    forum_id: 1 
    topic_id: 1 
    player_id: 2 
three: 
    id: 3 
    body: MyText 
    forum_id: 1 
    topic_id: 2 
    player_id: 3 

這裏是保持測試失敗:

test "should destroy post as author" do 
    sign_in players(:player2) 
    assert_difference('Post.count', -1) do # Line 41 
    delete :destroy, :id => posts(:one) 
    end 
    assert_redirected_to topic_url(assigns(:topic)) 
end 

這裏是我得到的錯誤:

1) Failure: test_should_destroy_post_as_author(PostsControllerTest) [../test/functional/posts_controller_test.rb:41]: 
"Post.count" didn't change by -1. 
<2> expected but was <3>. 

我將不勝感激任何幫助。當我確定答案是簡單的,我錯過了的時候,我感覺自己正在撞牆。提前致謝。

回答

0

我不知道爲什麼這個特定的措辭不起作用,但我修復它,以便當我像在控制檯中那樣銷燬帖子時,測試通過。

相反的:delete :destroy, :id => @post

我用:Post.destroy(@post)

+1

據我所知,這打破了測試的意圖,因爲你不再發送到控制器的請求。 – sscirrus 2013-11-01 08:36:41