gpt4 book ai didi

ruby-on-rails-3.1 - Rails 3.1.3 和继承资源测试失败

转载 作者:行者123 更新时间:2023-12-04 07:49:23 24 4
gpt4 key购买 nike

我将 Rails 3.1.3 用于具有继承资源 1.3.0 的项目。

当我有一个这样的 Controller 时:

class PostsController < InheritedResources::Base
end

我用 rspec 测试以下内容
  describe "PUT update" do
describe "with invalid params" do
it "re-renders the 'edit' template" do
post = Post.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Post.any_instance.stub(:save).and_return(false)
put :update, {:id => post.to_param, :post => {}}, valid_session
response.should render_template("edit")
end
end
end

我收到以下错误:
  3) PostsController PUT update with invalid params re-renders the 'edit' template
Failure/Error: response.should render_template("edit")
expecting <"edit"> but rendering with <"">
# ./spec/controllers/posts_controller_spec.rb:115:in `block (4 levels) in <top (required)>'

为什么是这样?我是否必须剔除其他东西?

最佳答案

只需添加这个:

Post.any_instance.stub(:errors).and_return(['error'])

紧随其后:
Post.any_instance.stub(:save).and_return(false)

关于ruby-on-rails-3.1 - Rails 3.1.3 和继承资源测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9187547/

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