测试未通过

发布于 2024-12-01 04:36:59 字数 1738 浏览 0 评论 0原文

我正在使用 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) 无方法错误: 100:Fixnum 的未定义方法 tr' # ./app/models/part_type.rb:7:infix_quotation' # ./spec/models/part_type_spec.rb:10:in `block (2 level) in '

2) PartType 应接受 0 值进行引用 失败/错误:PartType.new(@attr.merge(:quotation => 0)).should be_valid 无方法错误: 0:Fixnum 的未定义方法 tr' # ./app/models/part_type.rb:7:infix_quotation' # ./spec/models/part_type_spec.rb:18:in `block (2 level) in '

在 0.06089 秒内完成 3个例子,2个失败

I'm using RSpec for tests and I don't know how to get this to green.

In this case, I have a model called "PartType" that holds an attribute called "quotation".

The value for quotation comes from a form, so it will be a string.

To demonstrate you can go to console and type:

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

but..

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

And this value can have decimals. So I needed to do a "type_cast".

I have this on my PartTypemodel:

before_validation :fix_quotation, :if => :quotation_changed?

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

This are working as expected BUT when go to tests, it fails.

Here is my 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

And finally the failing tests:

Failures:

1) PartType should create a instance given a valid attributes
Failure/Error: PartType.create!(@attr)
NoMethodError:
undefined method tr' for 100:Fixnum
# ./app/models/part_type.rb:7:in
fix_quotation'
# ./spec/models/part_type_spec.rb:10:in `block (2 levels) in '

2) PartType should accept 0 value for quotation
Failure/Error: PartType.new(@attr.merge(:quotation => 0)).should be_valid
NoMethodError:
undefined method tr' for 0:Fixnum
# ./app/models/part_type.rb:7:in
fix_quotation'
# ./spec/models/part_type_spec.rb:18:in `block (2 levels) in '

Finished in 0.06089 seconds
3 examples, 2 failures

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-12-08 04:37:00
  1. 你的 include? 片段是错误的,我在第一个中得到了 false,在第二个中得到了 true。
  2. before_validation 被执行,quotation_before_type_cast 预计是一个 String,但它是一个 Fixnum。将 100 更改为 '100',将 0 更改为 '0'
  1. Your include? snippets are wrong, I got false in the first, true in the second.
  2. before_validation is executed and quotation_before_type_cast is expected to be a String but it is a Fixnum. Change 100 to '100' and 0 to '0'.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文