我有一个功能测试一直失败,我不知道为什么。这是论坛的一部分,测试是为了确保帖子的作者被允许删除他们自己的帖子。
当我手动尝试时,我能够在控制台和浏览器中销毁帖子,但我无法弄清楚出了什么问题。
这是控制器的销毁操作:
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
这是 post.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>.
我将不胜感激任何帮助。当我确定答案很简单但我想念的东西时,我觉得我的头撞到了墙上。提前致谢。