我的rspec测试都运行良好,除了当我复制对象以测试属性的唯一性时。
模型如下:
应用程序/模型/driver.rb
1. class Driver < ActiveRecord::Base
2. attr_accessible :last_name, :first_name, :short_name
# irrelevant code
18. before_save do
19. last_name = last_name.gsub(' ', '-').capitalize!
20. if last_name.include? '-'
21. last_name[(last_name.index('-'))+1] = last_name[(last_name.index('-'))+1].capitalize!
22. end
23. end
24. before_save do
25. first_name = first_name.gsub(' ', '-').capitalize!
26. if first_name.include? '-'
27. first_name[(first_name.index('-'))+1] = first_name[(first_name.index('-'))+1].capitalize!
28. end
29. end
30. before_save { short_name.downcase! }
# more irrelevant code
59. validates :short_name, presence: true, length: { maximum: 10 },
60. uniqueness: { case_sensitive: false }
所以我通过复制驱动程序对象来测试short_name属性的唯一性,如下所示:
规范/模型/driver_spec.rb
require 'spec_helper'
describe Driver do
before do
@driver = Driver.new(last_name: "Driver", first_name: "Example", short_name: "exdrvr")
end
subject { @driver }
it { should respond_to(:last_name) }
it { should respond_to(:first_name) }
it { should respond_to(:short_name) }
it { should be_valid }
# a bunch of other tests, all of which work fine
describe "when short name is already taken" do
before do
driver_with_same_short_name = @driver.dup
driver_with_same_short_name.short_name = @driver.short_name.upcase
driver_with_same_short_name.save
end
it { should_not be_valid }
end
下面是我运行所有测试后得到的结果:
.................................F....
Failures:
1) Driver when short name is already taken
Failure/Error: driver_with_same_short_name.save
NoMethodError:
undefined method `gsub' for nil:NilClass
# ./app/models/driver.rb:19:in `block in <class:Driver>'
# ./spec/models/driver_spec.rb:128:in `block (3 levels) in <top (required)>'
Finished in 1.74 seconds
38 examples, 1 failure
Failed examples:
rspec ./spec/models/driver_spec.rb:130 # Driver when short name is already taken
所以,基本上所有的测试运行良好,除了当我复制驱动程序对象。然后last_name属性突然变为nil。我试着从模型文件的第18-23行注释出before_save块,当然,我只是得到了first_name相同的错误信息。知道是怎么回事吗?
发现问题:不是在我的规格,但在模型的before_save块。我替换了驱动程序中的第18-23行。rb
before_save { self.last_name = last_name.gsub(' ', '-').split('-').each { |x| x.capitalize! }.join('-') }
, first_name块也是如此。测试结果很好更重要的是,模型现在正在做我想让它做的事情。我想这就是测试的目的吧!