Ruby on rails Capybara/RSpec-检查数组的内容

Ruby on rails Capybara/RSpec-检查数组的内容,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,我想重构我的代码,从可能的三行代码重构为一行代码 以下是片段: # capture an array of the three stats: impressions/clicks/AVT stats = page.all('.sub') # check our array.... expect(stats[0]).to have_content("Impressions") expect(stats[1]).to have_content("Cli

我想重构我的代码,从可能的三行代码重构为一行代码

以下是片段:

    # capture an array of the three stats: impressions/clicks/AVT

    stats = page.all('.sub')

    # check our array.... 

    expect(stats[0]).to have_content("Impressions")
    expect(stats[1]).to have_content("Clicks")
    expect(stats[2]).to have_content("Average")
我需要一个平衡的水豚
have\u content
和RSpec
contain\u include

比如:

expect(stats).to contain_exactly("Impressions", "Clicks", "Average")
其结果是:

Failure/Error: expect(stats).to contain_exactly("Impressions", "Clicks", "Average")
   expected collection contained:  ["Average", "Clicks", "Impressions"]
   actual collection contained:    [#<Capybara::Element tag="div" path="/html/body/div[2]/div[2]/div/div[1]/div[1]/div[1]">, #<Capybara::Element tag="div" path="/html/body/div[2]/div[2]/div/div[1]/div[2]/div[1]">, #<Capybara::Element tag="div" path="/html/body/div[2]/div[2]/div/div[1]/abbr/div[1]">]
   the missing elements were:      ["Average", "Clicks", "Impressions"]
   the extra elements were:        [#<Capybara::Element tag="div" path="/html/body/div[2]/div[2]/div/div[1]/div[1]/div[1]">, #<Capybara::Element tag="div" path="/html/body/div[2]/div[2]/div/div[1]/div[2]/div[1]">, #<Capybara::Element tag="div" path="/html/body/div[2]/div[2]/div/div[1]/abbr/div[1]">]
Failure/Error:expect(stats)。精确地包含“印象”、“点击”、“平均值”)
预期集合包含:[“平均”、“点击次数”、“印象”]
包含的实际集合:[#,#,#]
缺少的元素是:[“平均”、“点击”、“印象”]
额外的元素是:[#,#,#]

如何重构它?

我认为这应该是可行的:

# capture an array of the three stats: impressions/clicks/AVT

stats = page.all('.sub').map(&:text)

expect(stats).to contain_exactly("Impressions", "Clicks", "Average")
更新

从Rspec 3开始,如果要查找部分匹配,可以执行以下操作:

expect(stats).to contain_exactly(
                                 a_string_matching(/Impressions/),
                                 a_string_matching(/Clicks/),
                                 a_string_matching(/Average/)
                                )

我认为这应该奏效:

# capture an array of the three stats: impressions/clicks/AVT

stats = page.all('.sub').map(&:text)

expect(stats).to contain_exactly("Impressions", "Clicks", "Average")
更新

从Rspec 3开始,如果要查找部分匹配,可以执行以下操作:

expect(stats).to contain_exactly(
                                 a_string_matching(/Impressions/),
                                 a_string_matching(/Clicks/),
                                 a_string_matching(/Average/)
                                )

我认为这应该奏效:

# capture an array of the three stats: impressions/clicks/AVT

stats = page.all('.sub').map(&:text)

expect(stats).to contain_exactly("Impressions", "Clicks", "Average")
更新

从Rspec 3开始,如果要查找部分匹配,可以执行以下操作:

expect(stats).to contain_exactly(
                                 a_string_matching(/Impressions/),
                                 a_string_matching(/Clicks/),
                                 a_string_matching(/Average/)
                                )

我认为这应该奏效:

# capture an array of the three stats: impressions/clicks/AVT

stats = page.all('.sub').map(&:text)

expect(stats).to contain_exactly("Impressions", "Clicks", "Average")
更新

从Rspec 3开始,如果要查找部分匹配,可以执行以下操作:

expect(stats).to contain_exactly(
                                 a_string_matching(/Impressions/),
                                 a_string_matching(/Clicks/),
                                 a_string_matching(/Average/)
                                )

工作起来很有魅力,但现在我想我只是为自己创造了更多的工作,因为这3个关键字是一个更大的动态字符串的一部分,因此不会在“contain_justice”断言中传递a。IE“Overall Impressions 0”。工作起来很有魅力,但现在我想我只是为自己创造了更多的工作,因为这3个关键字是更大、动态字符串的一部分,因此不会在“contain_justice”断言中传递a。IE“Overall Impressions 0”。工作起来很有魅力,但现在我想我只是为自己创造了更多的工作,因为这3个关键字是更大、动态字符串的一部分,因此不会在“contain_justice”断言中传递a。IE“Overall Impressions 0”。工作起来很有魅力,但现在我想我只是为自己创造了更多的工作,因为这3个关键字是更大、动态字符串的一部分,因此不会在“contain_justice”断言中传递a。即“总体印象0”。