【问题标题】:Minitest a controller for a nested resourceMinitest 嵌套资源的控制器
【发布时间】:2015-08-08 04:53:54
【问题描述】:

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

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


我的尝试
我尝试弄乱我的路由(命名空间、模块等)并在我的测试中明确编写获取请求,但没有找到解决方案。我什至尝试生成一个脚手架以查看应用程序如何创建测试,但无济于事。我还浏览了博客和书籍(Makandra 的“Growing Rails Applications”,Michael Hartl 的教程),也没有找到任何东西。

config/routes.rb

Rails.application.routes.draw do
  resources :blogs do
    resources :posts
  end
end


controllers/posts_controller.rb

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

测试/posts_controller_test.rb

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


有什么建议么?如果我添加了一个文件夹结构,我将帖子文件放在博客文件夹下(如下),我是否需要以不同的方式编写我的控制器测试?我知道这对于这样一个简单的应用程序来说可能是矫枉过正,但我​​也想在另一个应用程序的整体设计模式中实现这些原则。

app/controllers/blogs
app/controllers/blogs_controllers
app/controllers/blogs/posts_controllers

【问题讨论】:

    标签: ruby-on-rails design-patterns routes minitest nested-resources


    【解决方案1】:

    由于Post 嵌套在Blog 下,URLs 方案将有点像这样:

    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_should_show_post(传入blog_id参数)

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

    所以嵌套资源的想法是,如果没有对父资源的引用,子资源就不能存在,那么blog_id 将能够成为PostsController 上的必需参数

    【讨论】:

    • 谢谢!就是这样。我尝试用这个专门调用 url,但它不起作用:get :show, "/blog/#{@post.blog_id}/posts/#{@post.id}"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多