Rails 5 测试控制器显示动作,缺少必需的键:[:id] [英] Rails 5 testing controller show action, missing required keys: [:id]

查看:65
本文介绍了Rails 5 测试控制器显示动作,缺少必需的键:[:id]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 rails 5.1.5 和默认测试库,所以当我尝试在帖子控制器中测试我的显示操作时,我收到一个错误:

i'm using rails 5.1.5, and default testing library, so when i try to test my show action in posts contoller, i get an error:

Error:
PostsControllerTest#test_should_show_post:
ActionController::UrlGenerationError: No route matches
{:action=>"show",   :controller=>"posts"}, missing required keys: [:id]

这是我的posts_controller_test.rb

here is My posts_controller_test.rb

require 'test_helper'

class PostsControllerTest < ActionDispatch::IntegrationTest

test "should get index" do
  get posts_url, params: { keywords: "" }
  assert_response :success
end

test "should show post" do
  post = posts(:post)
  get post_url, params: {id: post.id}
  assert_response :success
end

end

这是我的帖子夹具文件 posts.yml

Here is my post fixture file posts.yml

post:
  title: testpost
  body: lorem ipsum
  user: tom

routes.rb

Rails.application.routes.draw do
  root "posts#index"
  devise_for :users
  resources :posts do
    resources :comments
  end
  resources :users, except: [:create, :new]
  resources :tags, only: [:show, :create]
end

posts_controller.rb

posts_controller.rb

class PostsController < ApplicationController

  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :check_owner, only: [:destroy, :edit, :update]


  def index
    @posts = Post.search(params[:keywords]).uniq
  end

  def show
    #set_post
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    # link the post to user that logged in atm
    @post.user = current_user
    if @post.save
      # redirect to created post and show flash
      redirect_to @post, success: "Post successfully created !"
    else
      render :new
    end
  end

  def edit
    #set_post
    #check_owner
  end

  def update
    #set_post
    #check_owner
    if @post.update_attributes(post_params)
      # redirect to updated post and show flash
      redirect_to @post, success: "Post successfully updated !"
    else
      render :edit
    end
  end

  def destroy
    #set_post
    #check_owner
    @post.destroy
    # redirect to root and show flash
    redirect_to posts_path, success: "Post successfully deleted !"
  end


  private

    def set_post
      @post = Post.find(params[:id])
    end

    # strong params
    def post_params
      params.require(:post).permit(:title, :body, :image, :image_cache, :status, :adress, :current_tags)
    end

    def check_owner
      #check if current user is owner of the post
      redirect_to @post, alert: "You are not the owner of this post" unless current_user == @post.user || current_user.admin?
    end
end

最后是我的测试控制台

Running via Spring preloader in process 3663
Loading test environment (Rails 5.1.5)
2.4.1 :001 > User.all
User Load (0.3ms)  SELECT  "users".* FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: 1038054164, email: "test@example.org", created_at: "2018-03-13 16:49:29", updated_at: "2018-03-13   16:49:29", username: "Tomaskoz", avatar: nil, role: "user">]>
2.4.1 :002 > Post.all
Post Load (0.3ms)  SELECT  "posts".* FROM "posts" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Post id: 445279374, title: "testpost",  body: "lorem ipsum", created_at: "2018-03-13 16:49:29", updated_at: "2018-03-13 16:49:29", image: nil, user_id: 1038054164, status: true, adress:  nil>]>
2.4.1 :003 > 

我应该得到索引测试正常,但第二个没有,我尝试自己解决问题 2 天,但似乎不可能,请帮助我提前致谢!

My should get index test goes fine, but the second one doesn't, i've tried to solve the problem by myself for 2 days, but it's not seems to be possible, please help me thank in advance !

推荐答案

您在那里进行了集成测试.由于您使用了 url 助手,因此您必须将 ID 传递给助手.

You have an integration test there. Since you use the url helper you have to pass the ID into the helper.

test "should show post" do
  post = posts(:post)
  get post_url(post)
  assert_response :success
end

这篇关于Rails 5 测试控制器显示动作,缺少必需的键:[:id]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆