Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Jed Bradshaw
Jed Bradshaw
6,053 Points

Writing tests error. ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

I had everything working fine until today when I started on making the profile names unique. I had lots of errors then and fixed most of them but the following one kept coming back up. I rolled all the back to just having one test but this error still remains. Not sure what the problem is. Thanks.

Logging the error gets me this:

test/unit/user_test.rb:5:in `<class:UserTest>': undefined local variable or meth
od `user' for UserTest:Class (NameError)
        from test/unit/user_test.rb:3:in `<main>'


    ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ activerecord/fixtures/file.rb:60:in validate' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures/file.rb:47:inrows' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures/file.rb:29:in each' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:670:inblock (2 levels) in read_fixture_files' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures/file.rb:20:in open' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:669:inblock in read_fixture_files' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:668:in each' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:668:inread_fixture_files' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:548:in initialize' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:482:innew' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:482:in block (2 levels) in create_fixtures' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:479:inmap' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:479:in block in create_fixtures' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/connection_adapters/abstract_adapter.rb:168:indisable_referentia l_integrity' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:476:in create_fixtures' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:895:inload_fixtures' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/ active_record/fixtures.rb:849:in setup_fixtures' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.6/lib /active_support/callbacks.rb:407:in_run593860478setup861036642_callback s' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.6/lib /active_support/callbacks.rb:405:in __run_callback' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.6/lib /active_support/callbacks.rb:385:in_run_setup_callbacks' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.6/lib /active_support/callbacks.rb:81:in run_callbacks' C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.6/lib /active_support/testing/setup_and_teardown.rb:35:inrun'

1 Answer

Sandra Weber
Sandra Weber
1,531 Points

Hi Jed,

Can you please post your test code and your fixture? According to the stack trace that you posted, the error is on line 5 of your user_test.rb, but the stack trace doesn't give much more help than that. Thanks!

Also, you can format code on the forums to make it much easier to read by indenting each line 4 spaces (or a tab).

Jed Bradshaw
Jed Bradshaw
6,053 Points

Thanks for your response. My app is located on Dropbox here: https://www.dropbox.com/sh/nfzm31o95vffl74/W0w-0dP-1E

The code I'm using, in User_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase

test "a user should enter a first name" do
  user = User.new
  assert  !user.save
  assert !user.errors[:first_name].empty?
end

end

And the code in user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                :first_name, :last_name, :profile_name
  # attr_accessible :title, :body

validates :first_name, presence: true

  has_many :statuses

  def full_name
first_name + " " + last_name
  end
end

I don't have anything in my user.yml file. Thanks for your help.

Sandra Weber
Sandra Weber
1,531 Points

Thanks, that info helps a lot. Does deleting the empty user.yml fixture file fix the error? I believe it is trying to load the empty file, and empty files aren't "valid" fixture files so it is throwing that exception.

Jed Bradshaw
Jed Bradshaw
6,053 Points

That did not fix the problem, but I deleted the entire fixtures file. Now my test is passing.

I am not sure what I will do about that in the future, but I am happy its working now.

Jed Bradshaw
Jed Bradshaw
6,053 Points

Went through the Validating Uniqueness video again after deleting the fixtures file and redid all the steps, then managed to recreate my fixtures file and finally got everything to pass. Must have had some weird typo or something funny going on. Thanks for your help!