简体   繁体   中英

Devise user creation fails during Capybara integration testing (and Selenium webdriver)

I'm trying to create a user during an integration test to use for some operations. I'm using devise with :confirmable. The code is the following:

user = User.create({username: "user1", password: "pass1234", password_confirmation: "pass1234", email: "test@email.com"})
user.confirm!
fill_in "Username", :with => user.username
fill_in "Password", :with => user.password
click_button "Sign in"

The problem is that the login fails every time I try it. There are no errors about the user creation, but for some reason the user doesn't seem to "be there" when I try to login. I just get 'Invalid username or password' when I try to sign in. This seems like something to do with the fact that maybe Capybara/Selenium webdriver isn't waiting properly for the database operation to take place before it tries to sign in. If that's the case, how could I test it or fix it?

Is it "wrong" to even be trying to insert into the database during an integration test?

I don't use devise myself so can't really comment on the specifics of the problem you're encountering, but this question caught my eye:

Is it "wrong" to even be trying to insert into the database during an integration test?

Yes, I would say it generally is.

Your integration tests should test your code from the point of view of the user:

  • Expectations should only depend on what the user can actually see .
  • Actions should correspond only to what the user can actually do .

Inserting something into the database goes beyond the range of actions that the user has at their disposal. It is something for a unit test perhaps, but not for an integration test.

That being said, you could argue that seeding database data is a bit of an exception to this rule, since you're setting up context for your test (see my comments below).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM