gpt4 book ai didi

ruby-on-rails - 最小测试嵌套资源的 Controller

转载 作者:行者123 更新时间:2023-12-05 04:15:35 26 4
gpt4 key购买 nike

我想对嵌套资源 Controller 进行最小测试,但我不断收到以下错误:

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


我尝试过的
我尝试弄乱我的路由( namespace 、模块等)并在我的测试中明确编写获取请求,但没有找到解决方案。我什至尝试生成一个脚手架来查看该应用程序如何创建测试,但无济于事。我还浏览了博客和书籍(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

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


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

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

最佳答案

由于 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_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 上的必需参数

关于ruby-on-rails - 最小测试嵌套资源的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31889780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com