测试没有通过

时间:2011-08-23 06:34:16

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

我正在使用RSpec进行测试,我不知道如何将其变为绿色。

在这种情况下,我有一个名为“PartType”的模型,它包含一个名为“quotation”的属性。

引用的值来自表单,因此它将是一个字符串。

为了演示你可以去控制台并输入:

(1..1000).includes?("50") # false

,但..

(1..1000).includes?(50) # true

此值可以包含小数。所以我需要做一个“type_cast”。

我的PartType模型上有这个:

before_validation :fix_quotation, :if => :quotation_changed?

protected
  def fix_quotation
    self[:quotation] = quotation_before_type_cast.tr(' $, ' , '.' )
  end

这是按预期工作但是当进行测试时,它失败了。

这是我的part_type_spec.rb

require 'spec_helper'

describe PartType do

  before(:each) do
    @attr = { :title => "Silver", :quotation => 100 }
  end

  it "should create a instance given a valid attributes" do
    PartType.create!(@attr)
  end

  it "should accept null value for quotation" do
    PartType.new(@attr.merge(:quotation => nil)).should be_valid
  end

  it "should accept 0 value for quotation" do
    PartType.new(@attr.merge(:quotation => 0)).should be_valid
  end

end

最后是失败的测试:

Failures:

1)PartType应该创建一个给定有效属性的实例      失败/错误:PartType.create!(@ attr)      NoMethodError:        未定义的方法tr' for 100:Fixnum # ./app/models/part_type.rb:7:in fix_quotation'      #./spec/models/part_type_spec.rb:10:in在'

中的块(2级)

2)PartType应接受0值作为报价      失败/错误:PartType.new(@ attr.merge(:quotation => 0))。应该是be_valid      NoMethodError:        未定义的方法tr' for 0:Fixnum # ./app/models/part_type.rb:7:in fix_quotation'      #。/ spec / model / part_type_spec.rb:18:在'

中的块(2级)

以0.06089秒结束 3个例子,2个失败

1 个答案:

答案 0 :(得分:1)

  1. 你的include?代码片段错误,我在第一段时间是假的,在第二段时间是真的。
  2. before_validation已执行且quotation_before_type_cast应为String,但它是Fixnum。将100更改为'100',将0更改为'0'
相关问题