[Rails] Test of star evaluation function using Raty [Rspec]

Introduction

I implemented the evaluation function using Raty, but I will leave it as an article because it took time to execute the integration test. If you know it, there is nothing wrong with it, but I hope it helps beginners.

Evaluation function

Screen Shot 2020-12-13 at 17.39.34.png Evaluation function implemented using [Raty](https://github.com/wbotelhos/raty). I will omit the implementation method, but at the end I will introduce an article that seems to be helpful, so if you want to try it, please do.

I want to make the action of clicking the star mark of each item, so I will look at the elements using the verification tool.

Verification

Screen Shot 2020-12-13 at 17.40.05.png I see, you just have to find the img tag and click it.

rspec.rb


find("img").click

error

Screen Shot 2020-12-13 at 17.55.09.png Obviously, I was angry with "Clear which img it is". Since there are other img tags, we need to make it possible for the program to determine which element it is.

Correct code

rspec.rb


find('#taste_star').find("img[alt='5']").click

Finally, I was able to clear the test with this code ◎ Let's look at each one.

Overuse of find

I was personally blind because I usually specified a unique id for each element and could only pick up the element in one shot, but find can be used repeatedly. First, by picking up the id = "taste_star" specified in the parent element, you can narrow down the applicable range of the img tag that follows it.

Element [attribute]

Since there are still multiple img tags in the same parent element, use the alt attribute that is sorted individually. If you want to specify the attribute, you can enclose it in square brackets.

click Then click on the image and you're done!

By the way, you can use click_on to click a link or button, but in the case of an image, specify the alt attribute. In this case, the value of the alt attribute is not unique, so you will get an error.

Finally

While summarizing the article, I regret that I stumbled in a simple place again.

reference

Introduction of jquery.raty rails Add star function using "Raty" in Rails Introduction to RSpec that can be used, Part 4 "Any browser operation is free! Reverse lookup Capybara dictionary"

Recommended Posts

[Rails] Test of star evaluation function using Raty [Rspec]
Implement star rating function using Raty in Rails6
[Rails] Test code using Rspec
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Note] Summary of rails login function using devise ①
[Rails] Test with RSpec
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
Search function using [rails] ransack
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] About Rspec response test
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Implementation of new registration function in wizard format using devise
[Rails] Implementation of CSV import function
[Rails] Implementation of image preview function
User evaluation using the like function
[Rails] Tag management function (using acts-as-taggable-on)
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] Sign-up function using devise error number of arguments (given 0, expected 1)
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails5] Rspec -Unit test when nesting-
Implement star evaluation function in Rails 6 (Webpacker is installed as standard)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[RSpec] Unit test (using gem: factory_bot)
Let's unit test with [rails] Rspec!
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Rails] Addition of Ruby On Rails comment function
Introduction memo of automatic test using Jenkins
Implementation of user authentication function using devise (2)
[Ruby on Rails] View test with RSpec
Test the integrity of the aggregation using ArchUnit ②
Rails Addition of easy and easy login function
[Ruby on Rails] Controller test with RSpec
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Ruby on Rails] Model test with RSpec
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
Test the integrity of the aggregation using ArchUnit ③
Multiple image upload function using Rails Carrierwave
[Ruby On Rails] Error in test using RSpec MySQL client is not connected
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Implementation of Ruby on Rails login function (Session)
I rewrote the Rails tutorial test with RSpec
Create authentication function in Rails application using devise
[Ruby on Rails] Until the introduction of RSpec
[Rails] Display of multi-layered data using select boxes