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

ruby-on-rails – Rails respond_with&Rspec控制器:测试不

发布时间:2020-12-17 03:14:12 所属栏目:百科 来源:网络整理
导读:我正试图在Rails控制器中使用respond_to切换到respond_with.一切顺利,除了测试控制器规格中的无效保存.这是一个例子: 描述MyController做的 … describe "PUT update" do context "with invalid attributes" do it "should re-render the edit page" do sty
我正试图在Rails控制器中使用respond_to切换到respond_with.一切顺利,除了测试控制器规格中的无效保存.这是一个例子:

描述MyController做的

describe "PUT update" do
    context "with invalid attributes" do
      it "should re-render the edit page" do
        style = stub_model(Style)
        Style.stub(:find) { style } 
        Style.any_instance.stub(:save).and_return(false)
        put :update
        response.should render_template(:edit)
      end
    end
  end
end

这与我的旧的respond_to样式更新操作一样正常,但是使用respond_with,我得到了

Failure/Error: response.should render_template(“edit”)

所以,简而言之 – 我该如何测试? …或者我应该假设render_with知道它在做什么而不是测试?有什么一般建议?

提前干杯

PS:更新动作:

def update
    @style = Style.find(params[:id])
    flash[:notice] = "Style updated" if @style.update_attributes(params[:style])
    respond_with(@style)
  end

解决方法

我一直在研究这个问题(我是如何找到这个主题的) – 到目前为止,我有以下内容:

Location.any_instance.stub(:valid?).and_return(false)
Location.any_instance.stub(:errors).and_return('anything')

(其中Location是我使用respond_with的模型)

但我相信必须有一个更好的方法 – 如果我找到它,我一定会发布它!

注意:我也在使用响应者宝石,所以如果您不使用它,可能不需要其中一行!

(编辑:李大同)

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

    推荐文章
      热点阅读