Rails Tutorial Chapter 13 I don't know where the test code is testing

Asked 1 years ago, Updated 1 years ago, 364 views

About the user micropost in Chapter 13 of the Rails tutorial.

List 13.28: The [email protected]_s, response.body of the test to the User profile screen is the List 13.24:You may be testing <h3>Microposts(<%[email protected]%>)</h3> of the to add the microposts to the user's show page (profile screen), but the test results remain GREEN after commenting out the latter code.Which code does the former code test?

As a trial, I changed the test code to assert_match "Microposts(#{@user.microposts.count})", response.body, and when I commented out, the test result was RED.If you put it back, it will be GREEN.

I can't tell the difference between the two test codes.Thank you for your cooperation.

ruby-on-rails ruby

2022-12-17 15:17

3 Answers

The explanation of the tutorial seems to be the answer.

-

Enter a description of the image here


2022-12-18 05:11

We assume that what we would like to test in this test case is that the number of cases in the Microposts section is correct, as the questioner said.On the other hand, with this test code, even if you don't meet the requirements you want to test, you are likely to inadvertently pass the test.

If assert_match is used, any given string (this time response.body) will pass the test if it exists.For example, if @u ser.microposts.count.to_s is "1", the test will pass if the number "1" is included anywhere.I'm looking to see if it matches everything in HTML this time, so it matches one of <h1> for example.

This is how you want to test, but the conditions are often too lenient to test well.Since it is often difficult to write test codes that accurately describe the conditions, it is up to the implementer to decide which code is acceptable.

There are several ways to do a better test.In this case, the questioner may want to see a match on a longer string as already tried, or, in combination with assert_select, to make sure that there is a specific string under a specific tag.It's not a Rails standard feature, but snapshot testing is also famous.For more information on the features of the Rails standard, see the testing guide in the Rails guide: https://railsguides.jp/testing.html#%E3%83%93%E3%83%A5%E3%83%BC%E3%82%92%E3%83%86%E3%82%B9%E3%83%88%E3%81%99%E3%82%8B


2022-12-18 07:32

I just checked the source of the page and found that some of the digest of asset filenames contained the same number as @u ser.microposts.count.to_s ("34" for myself).
Answers and solutions to questions are given by other respondents.


2022-12-18 10:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.