Home > Blockchain >  View testing for html attributes in rspec
View testing for html attributes in rspec

Time:04-13

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.

CodePudding user response:

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.

  • Related