简体   繁体   English

IntegrationTest Ruby on Rails中的assert_select

[英]assert_select in IntegrationTest Ruby on Rails

I come from a Python and Java background with only basic knowledge to CSS, HTML, Ruby and trying to learn web development using Ruby on Rails. 我来自Python和Java背景,仅具有CSS,HTML,Ruby的基础知识,并且尝试使用Ruby on Rails学习Web开发。 I'm trying to follow the tutorial on Michael Hartl . 我正在尝试遵循Michael Hartl的教程。 I constantly have a problem reading the correct API or even understanding what the API is doing when I do as it seems there's a lot of ways a certain method could be called and examples are not exhaustively shown there. 我经常在阅读正确的API或什至了解API在做什么时遇到问题,因为似乎有很多方法可以调用某种方法,并且在那里没有详尽地显示示例。 In particular, I do not understand how to what arguments the assert_select method in Listing 7.25 is doing. 特别是,我不明白清单7.25中的assert_select方法在做什么参数。

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, params: { user: { name:  "",
                                         email: "user@invalid",
                                         password:              "foo",
                                         password_confirmation: "bar" } }
    end
    assert_template 'users/new'
    assert_select 'div#<CSS id for error explanation>'
    assert_select 'div.<CSS class for field with error>'
  end
  .
  .
  .
end

By referring to Online APIs , if one were to use assert_select without code blocks, we will use 2 arguments. 通过参考Online API ,如果其中一个使用不带代码块的assert_select ,我们将使用2个参数。 However, there is only 1 here in each of the 2 examples, with # and . 但是,两个示例中的每个示例中只有一个,其中#. , and I'm not too familiar whether this is some Rails/Ruby/CSS syntax, the origins of the syntax seems mixed. ,对于这是某些Rails / Ruby / CSS语法,我不太熟悉,该语法的起源似乎参差不齐。

Can someone enlighten me what am I missing out here? 有人可以启发我,我在这里错过了什么? I'm not too familiar when to use # and when to use . 我不太熟悉何时使用#和何时使用. , and how we can "cut short" the number of arguments. ,以及如何“缩短”参数数量。 There seems to always be a lot of play on punctuations like _ which are not shown here. 似乎在_等标点符号上经常有很多地方,此处未显示。

assert_select can be called several different ways , including with just a CSS selector, like the code in your question. 可以使用几种不同的方法调用 assert_select ,包括仅使用CSS选择器,例如您问题中的代码。

CSS selectors can contain # and . CSS选择器可以包含#. symbols. 符号。 Here's what they mean: 它们的含义如下:

  • # references an HTML element id #引用HTML元素ID
  • . references a CSS class 引用CSS类

Ids must be unique within the document. ID在文档中必须唯一。 So, div#error would find this exact element: <div id="error">Some error text</div> . 因此, div#error会找到以下确切元素: <div id="error">Some error text</div>

Classes on the other hand can be reused throughout the HTML document. 另一方面,可以在整个HTML文档中重用类。 So, div.error would find all elements like this: <div class="error">Some error text</div> . 因此, div.error将找到所有类似这样的元素: <div class="error">Some error text</div>

Finally, for what it's worth, # and . 最后, #和表示其价值. are also used in Ruby to differentiate between class and instance methods. 在Ruby中也用于区分类和实例方法。 . denotes a class method. 表示类方法。 # denotes an instance method. #表示实例方法。 But, that's not what's going on in the assert_select method. 但是,这不是assert_select方法中发生的事情。

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

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