Ruby on rails 水豚登录测试

Ruby on rails 水豚登录测试,ruby-on-rails,ruby,rspec,capybara,Ruby On Rails,Ruby,Rspec,Capybara,我想测试一个页面的登录是否有效,所以我创建了两个测试,一个是url是否改变,另一个是内容是否改变 我的看法是: <%= f.email_field :email, :autofocus => true, placeholder: "Email" %> <%= f.password_field :password, placeholder: "Password" %> <%= f.submit "Login" %> 测试: expect(page).t

我想测试一个页面的登录是否有效,所以我创建了两个测试,一个是url是否改变,另一个是内容是否改变

我的看法是:

<%= f.email_field :email, :autofocus => true, placeholder: "Email" %>
<%= f.password_field :password, placeholder: "Password" %>
<%= f.submit "Login" %>
测试:

 expect(page).to have_content('Welcome')
结果:应在“登录”中找到文本“欢迎”

结果:预期:“/home”得到:/”

所有的测试都失败了,我不知道为什么,我使用了有效的登录,并且我可以成功地手动使用登录。

预计在“登录”中会找到文本“欢迎”,这些错误通常发生在页面加载不好但执行测试时。因此,您应该在测试中应用等待。以下代码应适用于您:

visit '/users/sign_in'
page.should have_content("Wait for some text which is diaplayed on the page") //waits for the sign in page to load
fill_in 'Email', with: email
fill_in 'Password', with: password
click_on 'Login'
page.should have_no_content("Wait for the text which is available in the sign in page but not on next page") //waits for the sign in page to disappear and open welcome page.
page.should have_content("Welcome")
注意:除了等待一些文本,您还可以等待css选择器


希望这有帮助:)

因为您的身份验证系统重定向到
根路径
(/)而不是
/home
。您还应该添加运行测试报告的错误。类似于
预期的eql('/home')的当前路径,但是是'/'
如果您需要比前面的注释提供的更多帮助,请添加您的身份验证控制器代码。作为旁注-使用
expect(当前路径)
在较新版本的capybara中使用eql(“/home”)是不好的做法-而使用
expect(第页)。使用当前路径(“/home”)
-当您移动到支持JS的驱动器expected”/?user[email]=email%40gmail.com和user[password]=123456和user[记住我]时,这将为您节省大量的错误=0&commit=login“to equal”/home“为什么将测试重定向到此url?nothing:/Failure/Error:page.should_no_content(“Please”)expected#has no_content(“Please”)若返回true,则返回false您可以尝试增加此等待元素显示的超时,此步骤完成后,在此行之前增加capybara默认等待时间,并将其重置为DefaultWaitTime。您还可以添加用于调试此问题的屏幕截图。
expect(current_path).to eql('/home')
visit '/users/sign_in'
page.should have_content("Wait for some text which is diaplayed on the page") //waits for the sign in page to load
fill_in 'Email', with: email
fill_in 'Password', with: password
click_on 'Login'
page.should have_no_content("Wait for the text which is available in the sign in page but not on next page") //waits for the sign in page to disappear and open welcome page.
page.should have_content("Welcome")