Search code examples
ruby-on-rails-3.2rspec-rails

rspec error when testing duplicated object [Rails]


My rspec tests all run fine except when I duplicate the object to test for the uniqueness of an attribute.

Here is the model:

app/models/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 }

So I test for the uniqueness of the short_name attribute by duplicating the driver object as follows:

spec/models/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

Here's what I get when I run all the tests:

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

So, basically all the tests run fine except when I duplicate the driver object. Then the last_name attribute is suddenly nil. I tried commenting out the before_save block from lines 18-23 in the model file, and of course I just got the same error message for first_name. Any idea what's going on?


Solution

  • Found the problem: not in my specs at all, but in the model's before_save blocks. I replaced lines 18-23 in driver.rb with

    before_save { self.last_name = last_name.gsub(' ', '-').split('-').each { |x| x.capitalize! }.join('-') }
    

    and likewise for the first_name block. The tests run fine fine now. More importantly, the model is now actually doing what I intended it to do. I guess that's what testing is for!