I am trying Ticket Price Modeling in Ruby. I'm writing tests in Rspec, but it's a hassle to enter the Rspec command in the terminal every time.
I want to run Rspec easily.
(1) I used a library called Rake. (2) All spec files can now be executed at once simply by entering the rake command in the terminal. ③ It has become easier.
You can write what you want to execute in Rakefile and easily execute the contents written in Rakefile with the rake command. You don't have to type long commands in the terminal.
├── lib
│ ├── user.rb
│ └── ticket.rb
│
├── spec
│ ├── user_spec.rb
│ └── ticket_spec.rb
│
│ ── Rakefile * Create in step 2.
gem install rake
Rakefile
① require "rspec/core/rake_task"
##Now with just the rake command,③ can be executed.
② task :default => :spec
##When the rake command is executed, the files under the rspec directory are automatically recognized as test files and the test is executed.
③ RSpec::Core::RakeTask.new(:spec)
(Supplemental explanation) -There is a code in here that the file under the spec directory is specified. ・ About ③, I learned the article here.
"::" is a call to a "class", "module" or "method". Or "::" identifies the location in a nested environment when resolving names. Simply put, identify the location. (It is difficult to explain the concept in an easy-to-understand manner) When calling the "RSpec" class → RSpec When calling the "Core" class or module or method in the "RSpec" class → RSpec :: Core When calling the "RakeTask" module in the "Core" module in the "RSpec" module → RSpec :: Core :: RakeTask
If you execute the rake command in the terminal, ** _ spec.rb
under the spec directory will be executed in a batch.
rake
Recommended Posts