testing - 澄清此默认 rspec Controller 测试并使其通过

标签 testing rspec ruby-on-rails-3.1

这是使用我创建的脚手架生成的默认测试。

  describe "PUT update", focus: true do
    describe "with valid params" do
      it "updates the requested purchase_order" do
        current_valid_attr = valid_attributes
        purchase_order = PurchaseOrder.create! current_valid_attr
        # Assuming there are no other purchase_orders in the database, this
        # specifies that the PurchaseOrder created on the previous line
        # receives the :update_attributes message with whatever params are
        # submitted in the request.
        # PurchaseOrder.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
        # put :update, :id => purchase_order.id, :purchase_order => {'these' => 'params'}
        PurchaseOrder.any_instance.should_receive(:update_attributes).with(current_valid_attr)
        put :update, :id => purchase_order.id, :purchase_order => current_valid_attr
      end

问题是我不明白它应该做什么并且我无法使用正确的属性让它通过。这是我运行测试时的错误

Failures:

      1) PurchaseOrdersController PUT update with valid params updates the requested purchase_order
         Failure/Error: put :update, :id => purchase_order.id, :purchase_order => current_valid_attr
           #<PurchaseOrder:0x007fe3027521e0> received :update_attributes with unexpected arguments
             expected: ({"id"=>nil, "supplier_id"=>1, "no"=>1305, "no_rujukan"=>"Guiseppe Abshire", "jenis"=>"P", "mula_on"=>Sat, 23 Aug 2003 14:11:42 MYT +08:00, "created_at"=>nil, "updated_at"=>nil})
                  got: ({"id"=>nil, "supplier_id"=>"1", "no"=>"1305", "no_rujukan"=>"Guiseppe Abshire", "jenis"=>"P", "mula_on"=>"2003-08-23 14:11:42 +0800", "created_at"=>nil, "updated_at"=>nil})

提前致谢。

有效属性

  def valid_attributes
    Factory.build(:purchase_order).attributes
  end

工厂

FactoryGirl.define do
  factory :purchase_order do
      association           :supplier
      sequence(:no)         { |n| Random.rand(1000...9999) }
      sequence(:no_rujukan) { |n| Faker::Name.name }
      sequence(:jenis)      { |n| PurchaseOrder::PEROLEHAN[Random.rand(0..3).to_i] }
      sequence(:mula_on)    { |n| Random.rand(10.year).ago }
    end
end

最佳答案

此测试检查当请求到达 PurchaseOrdersController#update 操作时,PurchaseOrder 模型之一的 update_attributes 方法是否被调用,并且请求参数是否正确传递给这个方法。

这里的问题(如错误消息所述)是使用具有所有 String 类型值的属性哈希调用 update_attributes。这是因为 update 操作中最有可能使用的 params 哈希值(包含所有请求参数)的所有值都是字符串。

另一方面,您的 current_valid_attr 哈希值包含不同类型的值,例如 FixnumTime。当比较预期值和接收值时,您会收到错误,因为,例如,no 属性预期为 Fixnum 1305,但在提交请求后,它被转换改为字符串,update_attributes 接收字符串 '1305'

解决该问题的方法之一是确保 current_valid_attr 中的所有值都是字符串:

no = 1305
mula_on = Time.now
# explicitly convert all attributes to String
current_valid_attr = { :no => no.to_s, :mula_on => mula_on.to_s }

已更新

paramify_values可在测试中使用的方法,将参数的哈希值转换为 Controller 在 params 哈希值中接收的相同形式。

关于testing - 澄清此默认 rspec Controller 测试并使其通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8622879/

相关文章:

ruby - Rspec:如何测试自己的产量

php - 有人知道 PHP 的 RSPec 之类的东西吗?

mysql - 如何使 Rails 3.1 在 Debian squeeze 上与 MySQL Server 5.1 一起工作?

testing - selectText Action 总是等到超时结束

java - 如何使用嵌套生成器编写 jqwik 生成器方法

scala - 测试 API REST SCALA

rspec - 如何在 rspec 中模拟实例变量

ruby-on-rails - Rails 3.1 在 View 中使用 iframe 会使布局停止渲染

ruby-on-rails-3.1 - 如何将字符串转换为数组

ios - TestFlight 不显示任何 iOS 构建