It is difficult to manually check all the various behaviors of an application. Therefore, by writing a test, it is possible to efficiently check whether the data is passed correctly and whether it behaves as expected.
The most famous Ruby test framework
There are several types of tests used in the Rails tutorial.
There is a unit test to check the operation of the model and view helper alone, a function test to check the call result of the controller / view, an integration test to check the behavior of the application across multiple controllers assuming the actual operation of the user, and the system In development, we often do the above tests.
Put the gem for rspec in the Gemfile
group :development, :test do
gem 'rspec-rails'
end
after,
$ bundle install
Let.
If necessary, include FactoryBot (formerly FactoryGirl). References: https://qiita.com/at-946/items/aaff42e4a9c2e4dd58ed
describe , it , expect You can use these to describe your test requirements.
For example, in a unit test (model test),
spec/company_spec.rb
RSpec.describe Account, type: :model do
describe 'company model' do
it 'Create' do
expect { create(:company) }.to change { Company.count }.by(1)
end
end
end
When an error occurs in Rspec, the text described in context, it is output. This will give you an idea of where the error occurred.
context You may branch the condition using context. You may create each of it For example, in the controller test
spec/company_spec.rb
RSpec.describe CompaniesController, type: :controller do
describe 'Get list' do
context 'If you are logged in' do
it 'Returns 200' do
//Test process
end
end
context 'If you are not logged in' do
it 'Returns a 400 error' do
//Test process
end
end
end
end
Classification | To describe |
---|---|
describe | Subject to test |
context | Describe the case classification of specific conditions |
it | Describe what the output is |
After describing the behavior as above, we will write the test.
let , let!
When using test data in the test, describe such processing. For example
spec/company_spec.rb
RSpec.describe CompaniesController, type: :controller do
describe 'Get list' do
context 'If you are logged in' do
it 'Returns 200' do
let(:company) { Company.create(name: 'Yamada Co., Ltd.', email: '[email protected]') }
#Test process
end
end
context 'If you are not logged in' do
it 'Returns a 400 error' do
#Test process
end
end
end
end
Create data in the part where you write the process as described above.
let let! is each
Classification | Evaluation timing |
---|---|
let | Processed when the method is first called |
let! | Processed before block execution |
In other words, in the above example
let(:company) { Company.new(name: 'Yamada Co., Ltd.', email: '[email protected]') }
At this point,: company has not been created and For example
process :show, method: :get, params: { company_id: company.id }
The difference is that it is created when company is called, as in.
An error may occur due to the difference in the timing of being called, so this difference is something I want to be aware of.
before / after If there is a process you want to perform before and after the test, describe the process here.
Data created with Rspec is basically deleted from the test database after testing.
I'm going to summarize what I personally mess with with Rspec. I would appreciate it if you could point out any mistakes!
https://qiita.com/jnchito/items/42193d066bd61c740612#context-%E3%81%A7%E6%9D%A1%E4%BB%B6%E5%88%A5%E3%81%AB%E3%82%B0%E3%83%AB%E3%83%BC%E3%83%97%E5%8C%96%E3%81%99%E3%82%8B https://qiita.com/uchiko/items/d34c5d1298934252f58f
Recommended Posts