【问题标题】:Can't fix ProductsController PUT update errors无法修复 ProductsController PUT 更新错误
【发布时间】:2014-07-12 10:22:14
【问题描述】:

我现在正在使用的创意工坊应用程序有问题。我无法修复测试中的最后两个错误。在我看来,应用程序在浏览器中运行良好。也许测试有问题?任何帮助将不胜感激。

两个错误:

1) ProductsController PUT update with valid params updates the requested product
 Failure/Error: Unable to find matching line from backtrace
   Exactly one instance should have received the following message(s) but didn't: update

2) ProductsController PUT update with invalid params re-renders the 'edit' template
 Failure/Error: response.should render_template("edit")
   expecting <"edit"> but rendering with <[]>

测试代码:

require 'spec_helper'
describe ProductsController do
  let(:category) { create(:category) }
  let(:valid_attributes) { { "title" => "MyString", "category_id" => category.id, "price" => 5.59,                
  "description" => "Lorem ipsum dolor sit amet"} }
  let(:valid_session) { {} }

  describe "PUT update" do
    let(:user) { build(:user) }
    before do
      sign_in user
      controller.stub(:user_signed_in?).and_return(true)
      controller.stub(:current_user).and_return(user)
      controller.stub(:authenticate_user!).and_return(user)
    end

    describe "with valid params" do
      it "updates the requested product" do
        product = Product.create! valid_attributes
        Product.any_instance.should_receive(:update).with({ "title" => "MyString" })
        put :update, { id: product.to_param, product: { "title" => "MyString" }, category_id:                    
        category.to_param }, valid_session
      end
    describe "with invalid params" do
      it "re-renders the 'edit' template" do
        product = Product.create! valid_attributes
        Product.any_instance.stub(:save).and_return(false)
        put :update, { id: product.to_param, product: { "title" => "invalid value" }, category_id:             
        category.to_param }, valid_session
        response.should render_template("edit")
      end
    end
  end
end

ProductsController#更新代码:

def update
  if self.product.update(product_params)
    redirect_to category_product_url(category, product), notice: 'Product was successfully  
    updated.'
  else
    render action: 'edit'
  end
end

【问题讨论】:

    标签: ruby-on-rails testing ruby-on-rails-4 rspec


    【解决方案1】:

    一般情况

    expecting <"edit"> but rendering with <[]>
    

    通常这意味着您期待渲染(例如在验证失败之后)并且您的控制器执行重定向(在成功保存模型之后)


    在你的代码中

    您在此处存根 save 方法:

    Product.any_instance.stub(:save).and_return(false)
    

    但是调用一个使用update方法的动作

    if self.product.update(product_params)
    

    因此操作成功 -> 您的控制器重定向 -> 您的“编辑”模板未呈现 -> 您的规范失败

    您的解决方案

    你应该存根valid?,而不是存根save,在这种情况下这是一个很好的做法

    Product.any_instance.stub(:valid?).and_return(false)
    

    【讨论】:

    • 感谢本杰明的回答,但遗憾的是它并没有解决我的问题。我遇到了和以前一样的错误。
    • 您仍然收到错误 #2? expecting &lt;"edit"&gt; but rendering with &lt;[]&gt;,你确认了吗?
    • 是的,没有任何改变。
    • 所以有些事情你没有告诉我们,因为你发布的带有我建议你修改的代码会起作用。
    猜你喜欢
    • 2015-06-12
    • 2015-07-24
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多