导轨功能测试不会破坏

时间:2012-07-29 03:45:47

标签: ruby-on-rails-3.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>.

我非常感谢任何帮助。当我确定答案很简单而我不在时,我觉得我正撞在墙上。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

而不是:delete :destroy, :id => @post

我用过:Post.destroy(@post)

相关问题