Rails 3.2: Failed Assertion uniqueness: true
All,

I am having trouble with testing a model,  I have a simple customer table that uses the following fields:  name:string, location:string, full_name:string, active:boolean.   I am using the full_name field as a hidden field that contains the following "#{name} #{location}" and testing the record for uniqueness on the full_name field.

The test "customer is not valid without a unique full_name" is that test that I am having an issue with, I am trying to test an insertion of a duplicate record.  If I remove the bang(!) in front of assert !customer.save the test will pass.  this is acting like the fixtures are not being loaded into the table prior to running the test,I am running rake test:units.  I have tried running the server in development mode and inserted two records using the scaffolding and the second insertion fails and reports an error "Full name has already been taken" which is the expected behavior. 

can anyone give me some guidance on where I have screwed up the test!

Thanks in advance

Reback

****
Model:

    class Customer < ActiveRecord::Base
      attr_accessible :active, :full_name, :location, :name

      validates :active, :location, :name, presence: true
      validates :full_name, uniqueness: true              # case I am trying to test

      before_validation :build_full_name
  
      def build_full_name
        self.full_name = "#{name} #{location}"
      end
  
    end

****
Test Fixture customers.yml

    one:
      name: MyString
      location: MyString
      full_name: MyString
      active: false

    two:
      name: MyString
      location: MyString
      full_name: MyString
      active: false

    general:
      name: 'Whoever'
      location: 'Any Where'
      full_name: ''
      active: true
****
Test Unit Helpers customer_test.rb

    require 'test_helper'

    class CustomerTest < ActiveSupport::TestCase
      # test "the truth" do
      #   assert true 
      # end
  
      fixtures :customers
  
      # Test fields have to be present
      test "customer fields must not be empty" do
        customer = Customer.new
        assert customer.invalid?
        assert customer.errors[:name].any?
        assert customer.errors[:location].any?
        assert_equal " ", customer.full_name     # This is processed by a helper function in the model
        assert customer.errors[:active].any?
      end

      # Test full_name field is unique
      test "customer is not valid without a unique full_name" do
        customer = Customer.new(
        name: customers(:general).name,
        location: customers(:general).location,
        full_name: customers(:general).full_name,
        active: customers(:general).active
        )
      
        assert !customer.save   # this is the line that fails
        assert_equal "has already been taken", customer.errors[:full_name].join(', ')
      end
  
    end


Rails 3.2: Failed Assertion uniqueness: true
===

All,

I am having trouble with testing a model,  I have a simple customer table that uses the following fields:  name:string, location:string, full_name:string, active:boolean.   I am using the full_name field as a hidden field that contains the following "#{name} #{location}" and testing the record for uniqueness on the full_name field.

The test "customer is not valid without a unique full_name" is that test that I am having an issue with, I am trying to test an insertion of a duplicate record.  If I remove the bang(!) in front of assert !customer.save the test will pass.  this is acting like the fixtures are not being loaded into the table prior to running the test,I am running rake test:units.  I have tried running the server in development mode and inserted two records using the scaffolding and the second insertion fails and reports an error "Full name has already been taken" which is the expected behavior. 

can anyone give me some guidance on where I have screwed up the test!

Thanks in advance

Reback

****
Model:

    class Customer < ActiveRecord::Base
      attr_accessible :active, :full_name, :location, :name

      validates :active, :location, :name, presence: true
      validates :full_name, uniqueness: true              # case I am trying to test

      before_validation :build_full_name
  
      def build_full_name
        self.full_name = "#{name} #{location}"
      end
  
    end

****
Test Fixture customers.yml

    one:
      name: MyString
      location: MyString
      full_name: MyString
      active: false

    two:
      name: MyString
      location: MyString
      full_name: MyString
      active: false

    general:
      name: 'Whoever'
      location: 'Any Where'
      full_name: ''
      active: true
****
Test Unit Helpers customer_test.rb

    require 'test_helper'

    class CustomerTest < ActiveSupport::TestCase
      # test "the truth" do
      #   assert true 
      # end
  
      fixtures :customers
  
      # Test fields have to be present
      test "customer fields must not be empty" do
        customer = Customer.new
        assert customer.invalid?
        assert customer.errors[:name].any?
        assert customer.errors[:location].any?
        assert_equal " ", customer.full_name     # This is processed by a helper function in the model
        assert customer.errors[:active].any?
      end

      # Test full_name field is unique
      test "customer is not valid without a unique full_name" do
        customer = Customer.new(
        name: customers(:general).name,
        location: customers(:general).location,
        full_name: customers(:general).full_name,
        active: customers(:general).active
        )
      
        assert !customer.save   # this is the line that fails
        assert_equal "has already been taken", customer.errors[:full_name].join(', ')
      end
  
    end

