简体   繁体   English

在Capybara集成测试(和Selenium Webdriver)过程中,设计用户创建失败

[英]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. 我正在用: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? 尝试登录时,我只会得到“无效的用户名或密码”。这似乎与Capybara / Selenium Webdriver可能没有在尝试登录之前未正确等待数据库操作有关。如果是这样,我该如何测试或修复它?

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: 我不使用devise自己,因此无法真正评论您遇到的问题的具体细节,但是这个问题引起了我的注意:

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). 话虽如此,您可能会争辩说,为数据库设置种子数据是该规则的一个例外,因为您正在为测试设置上下文(请参见下面的评论)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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