Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the capybara equivalent for Minitest's assert_select?

I am pretty new to testing so feel free to correct me if I got things wrong in my approach to this.

I am testing a Rails4 app with Minitest. As I have JS included in many pages I am using Capybara to have JS drivers in my tests, and also to be able to trigger JS Events and test the expected results. Here my confusion begins, as there are many ways to include Capybara.

Furthermore I found out that the "normal" assertion-style syntax won't work with Capybara. And here my confusion peaks. During my research I found that there is a Spec-style DSL for Capybara as well as a few more. Now I use the gem 'minitest-rails-capybara' (Github), and I tried to work with the methods/DSL in this list: RubyDoc

I tried to replace a simple assertion that tests for a HTML Element.

before: assert_select "title", "Study | Word Up"

now: page.has_selector?('title', :text => "Study | Word Up")

But no matter what I test for, the test always passes. How is that possible? I checked the server logs to see if I am on the right page. But everything seems fine, the passing test doesn't make sense.

like image 354
Flip Avatar asked Mar 19 '15 10:03

Flip


2 Answers

You need to use Capybara's assert_selector:

assert_selector "title", text: "Study | Word Up"

If you are trying to assert the document's title from the head element and not the body, then you need to use assert_title:

assert_title "Study | Word Up"
like image 95
blowmage Avatar answered Nov 14 '22 23:11

blowmage


You need to use assert :

assert page.has_selector?('title', :text => "Study | Word Up")

has_selector? returns a boolean and you need to check the value of this boolean.

like image 33
Baldrick Avatar answered Nov 14 '22 23:11

Baldrick