开发者

Tests are not passing

开发者 https://www.devze.com 2023-03-30 15:02 出处:网络
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\".

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)
  e开发者_如何学Cnd

  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:infix_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:infix_quotation' # ./spec/models/part_type_spec.rb:18:in `block (2 levels) in '

Finished in 0.06089 seconds 3 examples, 2 failures


  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'.
0

精彩评论

暂无评论...
验证码 换一张
取 消