导轨功能测试不会破坏

问题描述:

我有一个功能测试,保持失败,我不知道为什么。这是论坛的一部分,测试的目的是确保帖子的作者被允许删除他们自己的帖子。导轨功能测试不会破坏

我能够在控制台和浏览器中销毁帖子,当我手动尝试时,我无法弄清楚发生了什么问题。

这里是控制器的破坏作用:

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>. 

我将不胜感激任何帮助。当我确定答案是简单的,我错过了的时候,我感觉自己正在撞墙。提前致谢。

我不知道为什么这个特定的措辞不起作用,但我修复它,以便当我像在控制台中那样销毁帖子时,测试通过。

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

我用:Post.destroy(@post)

+1

据我所知,这打破了测试的意图,因为你不再发送到控制器的请求。 – sscirrus 2013-11-01 08:36:41