加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 如何存根:当rspec测试使用respond_with的控

发布时间:2020-12-17 04:00:13 所属栏目:百科 来源:网络整理
导读:我重构了我的OrgController以使用respond_with,现在控制器规范脚手架失败并显示以下消息: 1) OrgsController POST create with invalid params re-renders the 'new' template Failure/Error: response.should render_template("new") expecting "new" but
我重构了我的OrgController以使用respond_with,现在控制器规范脚手架失败并显示以下消息:

1) OrgsController POST create with invalid params re-renders the 'new' template
   Failure/Error: response.should render_template("new")
     expecting <"new"> but rendering with <"">

规范看起来像这样:

it "re-renders the 'new' template" do
 Org.any_instance.stub(:save).and_return(false)
 post :create,{:org => {}},valid_session
 response.should render_template("new")
end

我已经读过我应该存根:errors hash使它看起来像是一个错误.最好的方法是什么?

解决方法

使用在v3中引入的RSpec的新语法,存根看起来像

allow_any_instance_of(Org).to receive(:save).and_return(false)
allow_any_instance_of(Org).to receive_message_chain(:errors,:full_messages)
  .and_return(["Error 1","Error 2"])

相关的控制器代码看起来像

if org.save
  head :ok
else
  render json: {
    message: "Validation failed",errors: org.errors.full_messages
  },status: :unprocessable_entity # 422
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读