简体   繁体   English

Minitest的assert_select是什么类似的水豚呢?

[英]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. 我正在使用Minitest测试Rails4应用程序。 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. 由于我在许多页面中都包含JS,因此我在测试中使用Capybara来获取JS驱动程序,并且还能够触发JS事件并测试预期结果。 Here my confusion begins, as there are many ways to include Capybara. 在这里,我的困惑开始了,因为有许多方法可以包括Capybara。

Furthermore I found out that the "normal" assertion-style syntax won't work with Capybara. 此外,我发现“普通”断言式语法不适用于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. 在我的研究过程中,我发现Capybara还有一些Spec风格的DSL以及更多。 Now I use the gem 'minitest-rails-capybara' ( Github ), and I tried to work with the methods/DSL in this list: RubyDoc 现在我使用gem'minitest-rails-capybara'( Github ),我尝试使用此列表中的方法/ DSL: RubyDoc

I tried to replace a simple assertion that tests for a HTML Element. 我试图替换一个测试HTML元素的简单断言。

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

now: page.has_selector?('title', :text => "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. 但一切似乎都很好,通过测试没有意义。

You need to use Capybara's assert_selector : 你需要使用Capybara的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 : 如果您试图从head元素而不是body中声明文档的标题,那么您需要使用assert_title

assert_title "Study | Word Up"

You need to use assert : 你需要使用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. 返回一个布尔值,你需要检查这个布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM