OmarAguinaga
OmarAguinaga

Reputation: 727

Spec looking for specific HTML tag does not work

I'm working on a rails application and want to test using capybara, so far I had no problem but now that I'm implementing a markdown parsing feature I cant make my test pass.

I added this test

it "Shows makdown as HTML" do
  visit_wiki
  expect(page).to have_tag('pre')  //**EDITED** becauseof typo
end

And I can't make it work, visit_wiki is just a method to well... visit the wiki.

def visit_wiki
  visit topics_path
  click_link topic.name
  click_link sentence
  expect(current_path).to eq topic_wiki_path(@wiki.topic, @wiki.id)
end

I know that works because I've been using that in other parts of this project.

Here is also my Factory where I make sure to always have one pre HTML tag.

require 'random_data'
FactoryGirl.define do
  factory :wiki do
    title Faker::Lorem.sentence
    body  RandomData.random_md
    private false
    topic
    user
  end
end

//where random_md is

module RandomData
    def self.random_md
        md = []
        md << Faker::Markdown.headers
        md << Faker::Markdown.block_code
        rand(4..10).times do
            md << Faker::Markdown.random
        end
        md.join("\n")
    end
end

From that factory I'm able to seed data and display it correctly on my site. I even tried this from this website:

expect("<html>
  <body>
    <h1>some html document</h1>
  </body>
 </html>").to have_tag('body')

still, fails...

I guess I must be doing something wrom but don't know what, I would really appreciate any help. Thanks in advance!

EDIT This is how body looks:

<body>
  <div class="row">
    <div class="col-md-8">
      <h2>Labore</h2>
      .
      .
      .
      <div class="highlight">
        <pre>
            <span class="no">Quo</span> 
            <span class="n">voluptates</span>
            <span class="n">et</span> 
            <span class="n">et</span> 
            <span class="n">excepturi</span> 
            <span class="n">sit</span>
            <span class="o">.</span>
         </pre>
      </div>
      .
      .
      .
    </div>
  </div>
</body>
</html>

Upvotes: 0

Views: 244

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

have_tag is not Capybara provided and isn't what you should be using with Capybara. This is because the have_tag matcher provided by rspec-html-matchers, grabs the page source as a string at time of call, parses it with nokogiri, and then queries against it. That neuters the waiting/retrying behavior Capybara provides and will lead to unstable/non-working tests, not to mention the extra overhead that produces for every query. What you want is have_css/have_xpath/have_selector

expect(page).to have_css('pre')

A second issue in your test code is that you shouldn't be using current_path with the eq matcher, rather you should be using the have_current_path matcher which utilizes Capybaras waiting/retrying behavior

expect(page).to have_current_path(topic_wiki_path(@wiki.topic, @wiki.id))

Upvotes: 1

Related Questions