开发者

Where are the factory_girl records?

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 ther开发者_如何学JAVAe! 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).


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜