用于登录页面身份验证的水豚测试用例



>我在下面有水豚测试用例。

it "Testing login page with valid data" do
  fill_in 'email', with: 'kiran@gmail.com'
  expect(page).to have_selector("input[value='kiran@gmail.com']")#Checking values are inserted in email field
  fill_in 'password', with: 'Kiran.6565'
  expect(page).to have_selector("input[value='Kiran.6565']")#Checking values are inserted in password field
  click_button('submit')
  expect(current_path).to eql(patient_detail_path(4))
end

我正在检查登录页面,一旦电子邮件和密码字段匹配,它应该重定向到具有id字段值的patient_details_path。在上面的代码中,我指定了电子邮件和密码,可以正常工作,但问题出在测试用例中。预期结果:它应该重定向到另一个页面(patient_details_path),但它再次重定向到主页(/)。

Failures:
1) Login Page Interface Test login page with valid data
   Failure/Error: expect(current_path).to eql(patient_detail_path(4))
   expected: "/patient_details/4"
        got: "/"
   (compared using eql?)
 # ./spec/views/login_spec.rb:41:in `block (2 levels) in <top (required)>'
Finished in 1.08 seconds (files took 2.13 seconds to load)
14 examples, 1 failure

尝试了与堆栈溢出不同的解决方案,但对我没有任何用处。以下是尝试的不同解决方案。

#expect(current_path).to eql(patient_detail_path(4))
#expect(page).to have_current_path(patient_detail_path(4))

如果电子邮件和密码不匹配,它将抛出错误并再次重定向到登录页面。在我的场景中,即使电子邮件和密码有效,它也会抛出错误。如果我在我的测试用例中添加以下代码,它将通过测试用例。

#expect(page).to have_content "Invalid username/password combination"

任何人请帮助我,我是铁轨上的红宝石和水豚的新手。

我猜你尝试编写的测试应该写成类似

before :each do
  @user = # Create the required user with whatever method you're using
  @patient = # Create the required patient with whatever method you're using
end
it "Logs in with valid data" do
  visit(patient_detail_path(@patient)) # gets redirected to the login path
  fill_in 'email', with: 'kiran@gmail.com'
  fill_in 'password', with: 'Kiran.6565'
  click_button('submit')
  expect(page).to have_current_path(patient_detail_path(@patient))
end

这是一个一般的猜测,可能不是 100% 正确的(很难准确猜测你想做什么,因为你的问题中缺少一半的测试——之前的块),但一般部分应该在那里。 由于您的没有登录,我猜您实际上并没有使用给定的电子邮件和密码创建有效的用户,或者您没有创建 id 为 4 的患者(您真的不应该依赖在功能测试中测试特定的 ID 号)。

此外,在检查给定的路径/url 时,您应该始终使用 have_current_path 匹配器,因为它可以防止测试不稳定,并且由于它不是视图测试,因此不应该在 spec/views/login_spec.rb 中,更合适的是spec/features/login_spec.rb

在我看来,驱动程序在 Rails 有机会更新 URL 以反映新页面之前捕获了 URL。

执行导航更改后,很难断言 URL,因为可能会出现争用条件。我建议:

断言可以验证用户是否成功登录的其他一些信息。使用等待选项断言。

expect(page).to have_current_path(patient_detail_path(@patient), wait: 3)

最新更新