我不明白为什么我的测试如果 3 倍嵌套资源索引页失败。请帮忙
I don't understand why my test if failing for a 3x nested resource index page. Help please
我有一个 forums/topics/post 模型关系,我正在尝试为索引页测试它。
这是我的测试:
test "index" do
get forum_topic_posts_path
assert_response :success
end
这是我的 url 来自抽佣路线:
这是我的测试失败的地方:
PostsControllerTest#test_index:
ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"posts"} missing required keys: [:forum_id, :topic_id]
这是我的帖子控制器的索引操作:
def index
@posts = Forum.find(params[:forum_id]).topics.posts
end
这里似乎有什么问题?我已经通过 Forum.find(params) 选项指定了在哪里搜索帖子,并且 topic_id 应该包含在该查询中。虽然我不得不承认这写起来确实很麻烦而且我觉得好像有更好的方法来做到这一点,但我想不出任何其他方法。有人可以帮我查明问题出在哪里吗?我还需要指定特定的主题参数吗?
我
您缺少 GET 请求中所需的 ID。您需要有一个论坛和一个主题供参考,以便路径有效。错误消息的最后部分给出了错误提示:"missing required keys"。尽管您可能需要添加所需的属性来创建论坛和主题,但测试应该更像下面的内容。
test "GET #index" do
forum = Forum.create
topic = forum.topics.create
get forum_topic_posts_path(forum.id,topic.id)
assert_response :success
end
一些额外的想法:您确实需要在控制器操作中引用主题 ID 才能获取帖子。您的 index 操作不太符合惯例。三重嵌套可能是重构的标志,它应该发生在代码的某处(取决于上下文,我承认我不知道)。根据上面的测试,下面的操作应该会让你获得 200 分的成功。
def index
@forum = Forum.find params[:forum_id].to_s
@topic = @forum.topics.find params[:topic_id].to_s
@posts = @topic.posts
end
我有一个 forums/topics/post 模型关系,我正在尝试为索引页测试它。
这是我的测试:
test "index" do
get forum_topic_posts_path
assert_response :success
end
这是我的 url 来自抽佣路线:
这是我的测试失败的地方:
PostsControllerTest#test_index:
ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"posts"} missing required keys: [:forum_id, :topic_id]
这是我的帖子控制器的索引操作:
def index
@posts = Forum.find(params[:forum_id]).topics.posts
end
这里似乎有什么问题?我已经通过 Forum.find(params) 选项指定了在哪里搜索帖子,并且 topic_id 应该包含在该查询中。虽然我不得不承认这写起来确实很麻烦而且我觉得好像有更好的方法来做到这一点,但我想不出任何其他方法。有人可以帮我查明问题出在哪里吗?我还需要指定特定的主题参数吗?
我
您缺少 GET 请求中所需的 ID。您需要有一个论坛和一个主题供参考,以便路径有效。错误消息的最后部分给出了错误提示:"missing required keys"。尽管您可能需要添加所需的属性来创建论坛和主题,但测试应该更像下面的内容。
test "GET #index" do
forum = Forum.create
topic = forum.topics.create
get forum_topic_posts_path(forum.id,topic.id)
assert_response :success
end
一些额外的想法:您确实需要在控制器操作中引用主题 ID 才能获取帖子。您的 index 操作不太符合惯例。三重嵌套可能是重构的标志,它应该发生在代码的某处(取决于上下文,我承认我不知道)。根据上面的测试,下面的操作应该会让你获得 200 分的成功。
def index
@forum = Forum.find params[:forum_id].to_s
@topic = @forum.topics.find params[:topic_id].to_s
@posts = @topic.posts
end