RSpec从旧语法转换为新语法

时间:2014-12-22 15:13:52

标签: 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
  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

如何将2个期望行转换为单个行?类似的东西:expect(subject).to have_selector(“h1”)and_to have_content(“Sign up”)

2 个答案:

答案 0 :(得分:2)

这对我有用:

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

除了使用单引号外,与您表达的语法完全相同。 :)

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

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

答案 1 :(得分:0)

我找到了以下选项:

  1. 像这样使用xpath匹配器:

    it { expect(subject).to have_xpath("//h1[contains(., 'Sign up')]") }
    
  2. 编写自己的自定义匹配器: https://www.relishapp.com/rspec/rspec-expectations/v/2-4/docs/custom-matchers/define-matcher 您可以在其中使用现有的匹配器,如have_selector和have_content

  3. 撰写您的匹配器: https://relishapp.com/rspec/rspec-expectations/v/3-1/docs/composing-matchers