查看 rspec 中 html 属性的测试

View testing for html attributes in rspec

使用 rspec 视图,我想测试 html 元素是否具有所需的属性集:

我有以下看法:

app/views/reset/new.html.erb

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

和测试:

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

我想在测试中结合这两个期望,以便我可以检查元素 email 是否具有所需的属性。

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: Attribute selectors