用于排序的rspec测试不按我期望的方式工作。怎么了?

时间:2015-03-26 19:14:09

标签: ruby-on-rails rspec

这是我的rspec测试:

RSpec.describe "DisplayLinks", :type => :request do
  describe "GET links_path" do
    it "should have a list of links sorted by popularity order" do
      link1 = create(:link, clicks: 50)
      link2 = create(:link, clicks: 75)
      link3 = create(:link, clicks: 100)

      visit root_path

      within "div.all-links li:nth-child(1)" do
        expect(page).to have_content(link3.short_url)
      end

      within "div.all-links li:nth-child(2)" do
        expect(page).to have_content(link2.short_url)
      end

      within "div.all-links li:nth-child(3)" do
        expect(page).to have_content(link1.short_url)
      end
    end
  end
end

以下是我的观点:

<div class="all-links">
  <% @links.each do |link| %>
    <li>
      <%= link.short_url %>
    </li>
  <% end %>
</div>

因此link3应始终位于页面顶部。但是,如果我在第一次测试后将nth-child之后的数字更改为2,它仍会通过。所以我觉得我得到了误报。这是怎么回事?我做错了什么?

0 个答案:

没有答案