views:

252

answers:

4

I'm trying to perform an integration test via Watir and RSpec. So, I created a test file within /integration and wrote a test, which adds a test user into a base via factory_girl.

The problem is — I can't actually perform a login with my test user. The test I wrote looks as following:

...

before(:each)
  @user = Factory(:user)
  @browser = FireWatir::Firefox.new
end

it "should login"
  @browser.text_field(:id, "username").set(@user.username)
  @browser.text_field(:id, "password").set(@user.password)
  @browser.button(:id, "get_in").click
end

...

As I'm starting the test and see a "performance" in browser, it always fires up a Username is not valid error.

I've started an investigation, and did a small trick. First of all I've started to have doubts if the factory actually creates the user in DB. So after the immediate call to factory I've put some puts User.find stuff only to discover that the user is actually in DB. Ok, but as user still couldn't have logged in I've decided to see if he's present in DB with my own eyes.

I've added a sleep right after a factory call, and went to see what's in the DB at the moment. I was crushed to see that the user is actually missing there! How come? Still, when I'm trying to output a user within the code, he is actually being fetched from somewhere. So where does the records, made by factory_girl within a runtime lie? Is it test or dev DB? I don't get it.

I've 10 times checked if I'm running my Mongrel in test mode (does it matter? I think it does, as I'm trying to tun an integration test) and if my database.yml holds the correct connection specific data.

I'm using an authlogic, if that can give any clue (no, putting activate_authlogic doesn't work here).

A: 

Don't forget that RSpec is probably using transations when running the specs. RSpec will wrap the execution of the spec within a transaction and rollback at the end. It means you won't be able to see the record from outside that transaction (i.e. from another SQL connection).

If you want to ensure the user record is actually created by Factory Girl, you can do something like:

before(:each)
  @user = Factory(:user)
  User.find_by_username(@user.username).should_not be_nil
  @browser = FireWatir::Firefox.new
end
jbpros
Thanks, will keep that in mind.
gmile
A: 

Factory Girl is going to create temporary DB entries in your test database. Your tests database is going to be cleared out after each test.

Nicholas C
A: 

Somehow the solution went strange — I put factories to before(:all) block, and all the stuff worked as it should.

gmile
A: 

Be careful with Mocha and stubbing!

I just deleted my answer/question here, as I was fooled by Mocha to believe there were some transactional rollback problems with Factory(:user). It might help others, so I'll risk putting the source of the problems out here:

SomeDependentObject.any_instance.stubs(:valid?).returns(false)

I had 10 tests, introducing this one, broke all but one (by chance, due to exection order), yielding error in setup/before(:each) on Factory(:user). Wasted a lot of hours today on this one.

Ole Morten Amundsen