Search code examples
ruby-on-railsdesign-patternsroutesminitestnested-resources

Minitest a controller for a nested resource


I want to minitest a nested resource controller and I keep getting the error below:

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


What I've tried
I tried messing with my routes (namespace, module, etc.) and explicitly writing get requests in my tests but haven't found a solution. I even tried generating a scaffold to see how the app would create tests but to no avail. I also looked through blogs & books (Makandra's "Growing Rails Applications", Michael Hartl's tutorial) and didn't find anything either.

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

tests/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


Any suggestions? And if I added a folder structure where I put post files under a blog folder (below) do I need to write my controller test differently? I know this might be overkill for such a simple app but I also want to implement these principles in an overall design pattern for another app.

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

Solution

  • Since Post is nested under Blog, the URLs scheme will be somewhat like this:

    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
    

    the above is not exhaustive list(rake routes for full list) but I think this will give you an idea that params[:blog_id] is now a required param to access any of the posts. Based on your this newly gained insight, you will soon discover that you will need to rewrite test_should_show_post like this(passing in blog_id param)

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

    So the idea with nested resource is that the child resource can't exist without the reference to the parent resource, then blog_id will able to be required param on PostsController