Ruby on rails 小型测试嵌套资源的控制器

Ruby on rails 小型测试嵌套资源的控制器,ruby-on-rails,design-patterns,routes,minitest,nested-resources,Ruby On Rails,Design Patterns,Routes,Minitest,Nested Resources,我想对嵌套的资源控制器进行小型测试,但不断出现以下错误: 1) Error: PostsControllerTest#test_should_show_post: ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts", :id=>"980190962"} test/controllers/posts_controller_test.r

我想对嵌套的资源控制器进行小型测试,但不断出现以下错误:

  1) Error:
PostsControllerTest#test_should_show_post:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts", :id=>"980190962"}
    test/controllers/posts_controller_test.rb:28:in `block in <class:PostsControllerTest>'

controller/posts\u controller.rb

Rails.application.routes.draw do
  resources :blogs do
    resources :posts
  end
end
class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @blog = Blog.find(params[:blog_id])
      @post = @blog.posts.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:post_title, :body, :blog_id)
    end
end
require 'test_helper'

class PostsControllerTest < ActionController::TestCase
  setup do
    @post = posts(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:posts)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create post" do
    assert_difference('Post.count') do
      post :create, post: { blog_id: @post.blog_id, body: @post.body, post_title: @post.post_title }
    end

    assert_redirected_to post_path(assigns(:post))
  end

  test "should show post" do
    get :show, id: @post
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @post
    assert_response :success
  end

  test "should update post" do
    patch :update, id: @post, post: { blog_id: @post.blog_id, body: @post.body, post_title: @post.post_title }
    assert_redirected_to post_path(assigns(:post))
  end

  test "should destroy post" do
    assert_difference('Post.count', -1) do
      delete :destroy, id: @post
    end

    assert_redirected_to posts_path
  end
end

由于
Post
嵌套在
Blog
下,因此URL方案有点像这样:

POST /blogs/:blog_id/posts - CREATE
GET /blogs/:blog_id/posts - INDEX
DELETE /blogs/:blog_id/posts - DESTROY
SHOW /blogs/:blog_id/posts/:id - SHOW
以上不是详尽的列表(
rake routes
用于完整列表),但我认为这会让您了解
params[:blog_id]
现在是访问任何帖子所必需的参数。根据您最近获得的这一见解,您很快就会发现,您需要像这样重写
test\u show\u post
(传入
blog\u id
param)


因此,嵌套资源的想法是,如果没有对父资源的引用,子资源就不可能存在,那么
blog\u id
将能够成为
PostsController

上的必需参数,谢谢!就这样。我试着专门用这个来调用url,但没有成功:get:show,“/blog/{@post.blog#id}/posts/{@post.id}”
POST /blogs/:blog_id/posts - CREATE
GET /blogs/:blog_id/posts - INDEX
DELETE /blogs/:blog_id/posts - DESTROY
SHOW /blogs/:blog_id/posts/:id - SHOW
test "should show post" do
  get :show, id: @post, blog_id: @post.blog_id
  assert_response :success
end