查看 rspec 中 html 属性的测试

发布于 2025-01-20 14:58:57 字数 616 浏览 2 评论 0原文

使用RSPEC视图,我想测试HTML元素具有所需的属性集:

我有以下视图:

app/views/reset/new.html.erb

<%= text_field_tag :email, params[:email], required: true %>

和测试:

spec/views/reset/reset/reset_spec。 RB

RSpec.describe "authentications/reset/new" do

require "rails_helper"

RSpec.describe "authentications/reset/new" do
    it "email is required" do
     render
     expect(rendered).to have_selector("#email")
     expect(rendered).to have_xpath("//input[@required='required']")
  end
end

我想在测试中将两个期望结合起来,以便我可以检查元素电子邮件是否具有所需的属性。

Using rspec views, I want to test that an html element has the attribute required set:

I have the following view:

app/views/reset/new.html.erb

<%= text_field_tag :email, params[:email], required: true %>

and the test:

spec/views/reset/reset_spec.rb

RSpec.describe "authentications/reset/new" do

require "rails_helper"

RSpec.describe "authentications/reset/new" do
    it "email is required" do
     render
     expect(rendered).to have_selector("#email")
     expect(rendered).to have_xpath("//input[@required='required']")
  end
end

I want to combine the two expects in the test so that I can check that the element email has the required attribute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2025-01-27 14:58:57
require "rails_helper"

RSpec.describe "authentications/reset/new" do
  # use specify when "it" doesn't make grammatical sense
  specify "email is required" do
     expect(rendered).to have_selector('input[type="email"][required]')
  end
end

参见 mdn> mdn:attribute selectors

require "rails_helper"

RSpec.describe "authentications/reset/new" do
  # use specify when "it" doesn't make grammatical sense
  specify "email is required" do
     expect(rendered).to have_selector('input[type="email"][required]')
  end
end

See MDN: Attribute selectors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文