【问题标题】:Rspec failed with update in controllerRspec 在控制器中更新失败
【发布时间】:2011-10-21 18:04:45
【问题描述】:

这是客户控制器中更新的 rspec 错误:

 5) CustomersController GET customer page 'update' should be successful
     Failure/Error: put 'update', id => 1, :customer => {:name => 'name changed'}
       <Customer(id: integer, name: string, short_name: string, contact: string, address: string, country: string, phone: string, fax: string, email: string, cell:
string, sales_id: integer, test_eng_id: integer, safety_eng_id: integer, web: string, category1_id: integer, category2_id: integer, active: boolean, biz_status: str
ing, input_by_id: integer, quality_system: string, employee_num: string, revenue: string, note: text, user_id: integer, created_at: datetime, updated_at: datetime)
(class)> received :find with unexpected arguments
         expected: (1)
              got: ("1")
     # ./app/controllers/customers_controller.rb:44:in `update'
     # ./spec/controllers/customers_controller_spec.rb:41:in `block (3 levels) in <top (required)>'

这是 rspec 代码:

it "'update' should be successful" do

  customer = mock_model(Customer) 
  Customer.should_receive(:find).with(1).and_return(customer)
  customer.stub(:update_attributes).and_return(true)    
  put 'update', :id => 1, :customer => {:name => 'name changed'}
  response.status.should == 302 #redirect()
end

这是控制器中的更新:

  def update
    @customer = Customer.find(params[:id])
    if @customer.update_attributes(params[:customer], :as => :roles_new_update)
      if @customer.changed
        @message = 'The following info have been changed\n' + @customer.changes.to_s
        @subject ='Customer info was changed BY' + session[:user_name]
        notify_all_in_sales_eng(@message,@subject)
      end  

      redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updated successfaully!'
    else
      render 'edit', :notice => 'Customer was not updated!'
    end
  end

有什么想法吗?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 rspec2


    【解决方案1】:

    发布的值(并通过params 哈希访问)是字符串。更正测试的最简单方法是使用Customer.should_receive(:find).with("1").and_return(customer)

    请注意,我们现在将“1”(即字符串)作为预期参数,而不是 1(FixNum)。

    【讨论】:

    • 这里是使用 with("1") 后的错误: 5) CustomersController GET customer page 'update' 应该是成功的 Failure/Error: put 'update', :id => 1, :customer => {:name => 'name changed'} 模拟“Customer_1001”收到意外消息:更改为(无参数)# ./app/controllers/customers_controller.rb:46:in update' # ./spec/controllers/customers_controller_spec.rb:41:in block(3 个级别)在 '
    • 那是因为你在你的代码中调用了@customer.changed,但是还没有存根那个方法。您需要存根该方法,或使用真实模型实例,以便 @customer.changed 正确评估。
    • 是的,你是对的。像这样存根:customer.stub(:changed).and_return(false) 绕过发送电子邮件。但是遇到了另一个问题,即如何测试:redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updatedsuccessfaully!'?
    • 另一个问题。通过使用存根根据需要返回值,我们真的可以在代码中测试任何东西吗?或者只是为了自我安慰而让它过去。
    • 您应该可以使用response.should redirect_to(session[('page'+session[:page_step].to_s).to_sym]) 进行测试。您的测试 rspec 测试的工作方式是您在每个测试中只测试一件事。其他所有内容都被存根,以便每个测试都是独立的。所以你应该使用另一个测试来检查@customer.changed 是否正常工作。这样你就有了适当的测试覆盖率。
    【解决方案2】:

    所有参数都作为字符串传递,我相信 find 方法正在对整数进行隐式对话。要么在控制器中使用 to_i 使其显式化,要么将您的规范更改为期望字符串“1”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多