测试未通过
我正在使用 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:in
fix_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:in
fix_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 PartType
model:
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
fix_quotation'
# ./app/models/part_type.rb:7:in
# ./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
fix_quotation'
# ./app/models/part_type.rb:7:in
# ./spec/models/part_type_spec.rb:18:in `block (2 levels) in '
Finished in 0.06089 seconds
3 examples, 2 failures
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
include?
片段是错误的,我在第一个中得到了 false,在第二个中得到了 true。before_validation
被执行,quotation_before_type_cast
预计是一个String
,但它是一个Fixnum
。将100
更改为'100'
,将0
更改为'0'
。include?
snippets are wrong, I got false in the first, true in the second.before_validation
is executed andquotation_before_type_cast
is expected to be aString
but it is aFixnum
. Change100
to'100'
and0
to'0'
.