Nice to meet you. From April, I changed my job from the medical industry to a web engineer. Ruby experience is about 2 months.
It's been a month and a half since I joined the company, and in addition to minor corrections, I've been gradually developing new functions, and I'm feeling the growth little by little (I'm telling you that).
When developing a new function, I was told "Please write a test" and when I looked at the test code, I got a state of "I don't know how to write ...", so I gave priority to studying the test. I am. I somehow know (I think) under what conditions the test should be done, but I think it is necessary to get used to the notation peculiar to Rspec.
Currently, I am studying with reference to the following books and articles. Everyday Rails-Introduction to Rails Testing with RSpec Introduction to RSpec that can be used, Part 1 "Understanding the basic syntax and useful functions of RSpec" Test Driven Development
As a memorandum, I will leave the process of studying the test as an article little by little.
In the first place, if you can't trust the test you wrote, it won't start ...
――By writing automated tests steadily, you can develop new functions while confirming that existing functions are working as expected. -You can guarantee that the original function is not destroyed when refactoring.
--You can investigate the impact when changing versions such as ruby, rails, and gem updates.
—— Helps others understand the specification as it represents the details of the expected behavior. --It will be an opportunity to reconsider the specifications and interface
--Methods and actions that are difficult to write tests have so many roles, which gives you an opportunity to rethink your implementation.
――This is all. Development is much safer and more secure than manual testing and prayer deployment
--Add to Gemfile because it is not included in Rails by default → bundle install
Gemfile
group :development, :test do
gem 'rspec-rails', '~> 3.8.0'
end
--Creating a test DB
config/database.yml
test:
<<: *default
database: db/test.sqlite3
Run bin / rails db: create: all
Run bin / rails g rspec: install The spec storage directory, helper file, and configuration file are created.
.rspec
--format documentation
If you add it, the output information will be easier to read.
--Install binstub and spring to improve Rspec test efficiency
gemfile
group :development do
gem 'spring-commands-rspec'
end
Run $ bundle exec spring binstub rspec Now you can run the test with $ bin / rspec
--describe: Test grouping declaration, nestable --it: Organize tests into units called examples --expect (A) .to eq B: A test that expects A to be B
sample_spec.rb
Rspec.describe 'Calculation' do
it '100 +100 is 200' do
expect(100 + 100).to eq 200
end
it '100 *100 is 10000' do
expect(100 * 100).to eq 10000
end
end
--context: Group by condition --before: Data preparation and assets before test execution, which are always called before execution of example, are often performed. --Be careful when using variables because before and it have different scopes.
--let (: A) {B}: B can be referred to as A --let is lazy evaluation and will not be used until it is called --let !: The value defined before executing the example is created, this may be better when the test is failing due to lazy evaluation
--subject: Can group objects under test --In that case, use is_expected
--shared_example, it_behaves_like: Can be reused when example is not DRY
--shared_context, include_context: context can be reused
For the time being, this is the end. I understand the basics, so I will write more and more