views:

19

answers:

1

I added a unit test manually to test a library I'm building. I have some fixtures which are tested in their corresponding models without any issues.

The test case inherits from ActiveSupport::TestCase which runs fixtures :all.

require 'test_helper'
require 'mylib'

class MyLibTest < ActiveSupport::TestCase

  @user = User.find(1)

  test "Setup" do
    assert_equal(@user.id, 1)
  end

end

Even though user = User.find(1) works fine in every test that is actually for a model, this test raises an exception: /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:287:in 'find_one': Couldn't find User with ID=1 (ActiveRecord::RecordNotFound)

I have tried moving fixtures :all into the MyLibTest-class (suggested in another post) but with no difference.

How can I make my test work?

EDIT
I found out what was wrong, and it wasn't at all related to the fixtures. I wanted to use the same User object in several test cases so I added @user = User.find inside the class, but not inside a testcase. When I realized this and tried putting it in a test case, everything worked. Awkward :)

A: 

I can't be too sure exactly whats up without seeing how your Fixtures file is set up, but try these:

  1. Try moving "fixtures :all" to test_helper.rb.
  2. In the fixtures files, are you setting "id:" on each record? If not, the id is actually not going to be simply incremental - it is created based on the name of the fixture. Try using "User.first" instead of User.find(1). If you need the id to be 1, then set "id: 1" on the fixture.

Hope this helps.

vegetables
Hi, "fixtures :all" are in test_helper.rb, and yes id is set on the user fixture. What is really weird is that User.find(1) works wonderfully in user_test.rb but not in my_lib_test.rb. User.first returned nil.
Christoffer
Well, I found out what was wrong. Thank you for taking the time to help :)
Christoffer