gmich
gmich

Reputation: 150

How/why does a user sign_in integration test actually allow a successful fake user sign_in?

I am fairly new to rails. I am working through railstutorial (Rails 3.0). At one point, we write an integration test for the user sign in/sign out flow:

describe "success" do
  it "should sign a user in and out" do
    user = Factory(:user)
    visit signin_path
    fill_in :email,    :with => user.email
    fill_in :password, :with => user.password
    click_button
    controller.should be_signed_in
    click_link "Sign out"
    controller.should_not be_signed_in
  end
end

My understanding is that testing uses the db/test database. This database is empty, as far as I can tell (opened it using rails console test). How is it that we can create a fake user using Factory, and then sign successfully with that user's name and email?

RailsTutorial explains:

Here the before(:each) block signs in by visiting the signin page and submitting a valid email/password pair.

Why is it a valid pair? I am clearly missing something.

I am particularly interested in this because it looks to me like the sign in is NOT working, and I would like to figure out why.

Upvotes: 1

Views: 101

Answers (1)

Nash Bridges
Nash Bridges

Reputation: 2380

You're right, your test database is empty, because each test example is run inside a transaction, and all records created within current example are deleted via rollback. The purpose for that is to keep each example isolated from another one.

But when current example is runnning, your database is populated with records (one record in your case) made by FactoryGirl. Whatever email and password will be generated in factory, it will be persisted both in database and in memory (user variable). That's why user.password will always be correct.

Upvotes: 2

Related Questions