eeeeeean
eeeeeean

Reputation: 1834

User model test error from inserting into Group table

I'm just starting out with tests. When I run this one:

rake test test/models/user_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "should not save without an email address" do
    user = User.new
    assert_not user.save
  end
end

I get the following error:

1) Error: UserTest#test_should_not_save_without_an_email_address: ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: groups.name: INSERT INTO "groups" ("created_at", "updated_at", "id") VALUES ('2015-08-11 17:31:07', '2015-08-11 17:31:07', 980190962)

This is user.rb

class User < ActiveRecord::Base

  has_many :groups
  has_many :user_groups

  attr_accessor :password
  EMAIL_REGEX = /A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
  validates :password, :confirmation => true #password_confirmation attr
  validates_length_of :password, :in => 6..20, :on => :create
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX

  before_save :encrypt_password
  after_save :clear_password

  def encrypt_password
    if password.present?
      self.salt = Digest::SHA1.hexdigest("# We add {self.email} as unique value and #{Time.now} as random value")
      self.encrypted_password = Digest::SHA1.hexdigest("Adding #{self.salt} to {password}")
    end
  end

  def clear_password
    self.password = nil
  end

end

This is test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

As far as I can tell I don't have any callback or otherwise that would attempt to write to the "groups" table. My "groups.yml" is default, but that shouldn't matter if I'm only testing this one model, correct? Any help as to where I could start looking would be much appreciated. Thanks!

Upvotes: 1

Views: 32

Answers (1)

eeeeeean
eeeeeean

Reputation: 1834

test_helper.rb was setting up all my fixtures and they weren't defined. Commenting "fixtures :all" fixed it.

Upvotes: 2

Related Questions