views:

59

answers:

1

Hi - I'm trying out rspec, and immediately hit a wall when it doesn't seem to load db records I know exist. Here's my fairly simple spec (no tests yet).

require File.expand_path(File.dirname(__FILE__) + '../spec_helper')

describe SomeModel do

  before :each do
    @user1 = User.find(1)
    @user2 = User.find(2)
  end

  it "should do something fancy"
end

I get an ActiveRecord::RecordNotFound exception, saying it couldn't find User w/ ID=1 or ID=2, which I know for a fact exist. I set both test and development databases to point to the same schema in database.yml, so this shouldn't be database mixup. I also ran script/generate rspec after installing the gems (rspec, rspec-rails), and gem.config both environment.rb and test.rb. Any idea what I'm missing? thanks.

EDIT

Seems I was running the tests with rake spec:models, which emptied the db and thus no records were found. When I used % spec spec/models/some_model_spec.rb, everything worked as expected.

+2  A: 

You should not be relying on test data to be there in your database, as it could (and often does!) change. Secondly, you should not be joining your test and development database at the same database. Tests should run on a pure database, not one corrupted by your development mucking about.

What you should be doing is setting up data specifically for the tests in the form of fixtures, or by using a tool such as factory_girl or machinist.

Ryan Bigg