RSpec从旧语法转换为新语法

RSpec从旧语法转换为新语法,rspec,rspec2,Rspec,Rspec2,我想从旧的RSpec语法转换: describe "User pages" do subject { page } describe "signup page" do before { visit signup_path } it { should have_selector('h1', text: 'Sign up') } end end 对于新语法: RSpec.describe "UserPages", :type => :request do su

我想从旧的RSpec语法转换:

describe "User pages" do
  subject { page }
  describe "signup page" do
    before { visit signup_path }
    it { should have_selector('h1', text: 'Sign up') }
  end
end
对于新语法:

RSpec.describe "UserPages", :type => :request do
  subject { page }
  describe "signup page" do
    before { visit signup_path }

    it { expect(subject).to have_selector("h1") }
    it { expect(subject).to have_content("Sign up") }

  end
end

如何将两行expect转换为一行?类似于:expect(subject)。要使用选择器(“h1”)和内容(“注册”)

我找到了以下选项:

  • 像这样使用xpath匹配器:

    it { expect(subject).to have_xpath("//h1[contains(., 'Sign up')]") }
    
  • 编写自己的自定义匹配器: 您可以使用现有的匹配器,如have_选择器和have_内容

  • 组成你的配对者:

  • 这对我很有用:

    it { expect(subject).to have_selector('h1', text: 'Sign up') }
    
    除使用单引号外,与您表达的一般语法完全相同。:)

    或者,如果您喜欢完整xpath路径的特殊性:

    it { expect(subject).to have_selector(:xpath, '//div/h1', text: 'Sign up') }