Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rspec-在测试中处理参数的最佳方法是什么?_Ruby On Rails_Testing_Rspec - Fatal编程技术网

Ruby on rails Rspec-在测试中处理参数的最佳方法是什么?

Ruby on rails Rspec-在测试中处理参数的最佳方法是什么?,ruby-on-rails,testing,rspec,Ruby On Rails,Testing,Rspec,我对一个采用参数散列的方法进行了测试,如果未在参数中设置,则总数为0: 项目1.rb: def set_template_values(params) self.set_nil_values self.template = 1 self.deduction = 0 self.tax_amount = 0 self.tax_id = 0 self.tax_rate = 0 self.unclaimed_tax = 0 self.c

我对一个采用参数散列的方法进行了测试,如果未在参数中设置,则总数为0:

项目1.rb:

  def set_template_values(params)
    self.set_nil_values
    self.template = 1
    self.deduction = 0
    self.tax_amount = 0
    self.tax_id = 0
    self.tax_rate = 0
    self.unclaimed_tax = 0
    self.cost = 0
    self.total = 10  
      if params[:total].to_i > 0
        self.total = params[:total]
      end
  end
这是我的测试:

    let(:template_item){Item.new(:template_check => 'True' ,  
        :name => 'Gas' ,  
        :category_id => 1 ,  
        :sub_category_id => 24 ,  
        :job_id => 1, 
        :total => 10)}

  it 'Template Item' do
      template_item.set_nil_values
      template_item.set_template_values(template_item.attributes)
      template_item.save
      template_item.total.should be == 10
  end
我得到这个错误:

  1) Item Template Item
     Failure/Error: template_item.total.should be == 10
       expected: == 10
            got:    #<BigDecimal:7fb9f316e270,'0.0',9(36)>

但我宁愿在创建项目时设置所有参数,就像在现实世界中一样。是否有特定的方法传递参数?

是否为您的模型设置了任何验证?如果是这样,
template\u item.save
可能不是在保存模板项目。在
模板之后,您需要使用
调试器
gem或其他工具来检查
模板项目
。保存
以查看其属性值。当我将测试放在保存之前时,它仍然失败,因此根本没有设置它。它在生产中运行良好。此外,当我在参数中显式设置值时,它也会起作用,因此传递“template_item.attrubues”时会出现问题第一个问题:您使用的是“be==10”。摆脱==您已经在检查与“be”是否相等。@diego.greyrobot您确定吗?这会引发一个错误:使用equal?比较预期的#=>10个#=>#比较,它比较对象标识,但预期的和实际的不是同一个对象。如果您不关心本例中的对象标识,请使用
expect(实际)。来均衡(expected)
。#/spec/models/item_spec.rb:37:in‘block(2层)in’这不是一个错误,只是相等性检查失败,因为值不相同。您可以使用“be”、“eq”或“eq”。但不是像你现在做的那样两者都有。至于你考试不及格,我不敢肯定。尝试检查template_item.attributes的值。它是否包含您期望的:总数?检查方法:将模板_item.attributes.Inspect置于“应该”之上
  template_item.set_template_values(:total => 10)