gpt4 book ai didi

ruby-on-rails - 使用和测试 Rails ActionController 的 respond_with

转载 作者:行者123 更新时间:2023-12-02 02:29:32 28 4
gpt4 key购买 nike

提前为这个问题的冗长道歉。如果您耐心等待我,我想您会发现它实际上非常简单……鉴于我有限的 Rails 领域知识,我很难解释清楚。

鉴于 an ActionController commit dated Aug 6 中的这条评论:

 === Builtin HTTP verb semantics

Rails default renderer holds semantics for each HTTP verb. Depending on the
content type, verb and the resource status, it will behave differently.

Using Rails default renderer, a POST request for creating an object could
be written as:

def create
@user = User.new(params[:user])
flash[:notice] = 'User was successfully created.' if @user.save
respond_with(@user)
end

Which is exactly the same as:

def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(@user) }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

The same happens for PUT and DELETE requests.

我修改了一个非常基本的 Controller 以使用respond_with。当 Rails 自动生成的测试尝试将空的 params 传递给更新和创建方法时,除了 2 个规范失败外,一切似乎都运行良好。我可以用一个简单的 if save/else 来纠正这种行为,但我试图理解这个"new"功能。我认为默认规范可能是以过时的方式编写的。

来自提交评论:“由于请求是 POST,respond_with 将检查 @people 资源是否有错误。如果有错误,它将呈现具有不可处理实体状态 (422) 的错误对象。

因此向下滚动到 POST(下方)下的最后一个测试/规范,我是否可以重写它以测试“不可处理的实体状态 (422)”并通过,因此一切都很好?

我的 Controller :

class ClownsController < ApplicationController
respond_to :html, :json

def index
respond_with(@clowns = Clown.all)
end

def show
respond_with(@clown = Clown.find(params[:id]))
end

def new
respond_with(@clown = Clown.new)
end

def edit
respond_with(@clown = Clown.find(params[:id]))
end

def create
@clown = Clown.new(params[:clown])
flash[:notice] = 'Clown was successfully created.' if @clown.save
respond_with(@clown)
end

# Replacing def create above with this won't Fail the spec ##
#
# def create
# @clown = Clown.new(params[:clown])
# respond_with(@clown) do |format|
# if @clown.save
# flash[:notice] = 'Clown was successfully created.'
# format.html { redirect_to @clown }
# else
# format.html { render :action => :new }
# end
# end
# end


def update
@clown = Clown.find(params[:id])
flash[:notice] = 'Clown has been updated.' if @clown.update_attributes(params[:clown])
respond_with(@clown)
end

def destroy
@clown = Clown.find(params[:id])
flash[:notice] = 'Successfully deleted clown.' if @clown.destroy
respond_with(@clown)
end
end

测试规范:

$ rspec spec/
.......F....F..............

Failures:

1) ClownsController POST create with invalid params re-renders the 'new' template
Failure/Error: response.should render_template("new")
expecting <"new"> but rendering with <"">.
Expected block to return true value.
# (eval):2:in `assert_block'
# ./spec/controllers/clowns_controller_spec.rb:69:in `block (4 levels) in <top (required)>'

2) ClownsController PUT update with invalid params re-renders the 'edit' template
Failure/Error: response.should render_template("edit")
expecting <"edit"> but rendering with <"">.
Expected block to return true value.
# (eval):2:in `assert_block'
# ./spec/controllers/clowns_controller_spec.rb:107:in `block (4 levels) in <top (required)>'

这是 clowns_controller_spec.rb 的一部分:

require 'spec_helper'

describe ClownsController do

def mock_clown(stubs={})
(@mock_clown ||= mock_model(Clown).as_null_object).tap do |clown|
clown.stub(stubs) unless stubs.empty?
end
end

...

describe "POST create" do

describe "with invalid params" do
it "re-renders the 'new' template" do
Clown.stub(:new) { mock_clown(:save => false) }
post :create, :clown => {}
response.should render_template("new")
end
end

最佳答案

尝试用以下代码模拟你的 Clown 类

Clown.stub(:new) { mock_clown(:errors => {:any => 'error'}) }

这样 respond_with 方法就会知道模型保存失败并渲染新模板。

关于ruby-on-rails - 使用和测试 Rails ActionController 的 respond_with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4309300/

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