GitLab Testing and Debugging

Testing

GitLab uses RSpec as its testing framework. RSpec is a behavior-driven development (BDD) framework that allows you to describe the expected behavior of your code in a clear and concise way.

Running Tests

  • All tests: bundle exec rspec
  • Specific file: bundle exec rspec spec/models/project_spec.rb
  • Specific example: bundle exec rspec spec/models/project_spec.rb:10

Writing Tests

  • Example of a basic test:
require 'spec_helper'
          
          describe Project do
            it 'has a name' do
              project = create(:project)
              expect(project.name).to be_present
            end
          end
          

Code Coverage

  • Generate coverage report: bundle exec rspec --format documentation --profile
  • View report: open coverage/index.html

Debugging

Debugging Tools

  • Pry: A powerful interactive debugger that allows you to inspect variables, call methods, and step through your code.

Example of using Pry:

require 'pry'
          
          describe Project do
            it 'has a name' do
              project = create(:project)
              binding.pry
              expect(project.name).to be_present
            end
          end
          

Setting Breakpoints

  • To set a breakpoint, you can use the binding.pry command.
  • After you hit a breakpoint, you can use commands like next, step, and continue to navigate through your code.

Logging

  • GitLab uses the Rails.logger to log information about your application.
  • You can use the Rails.logger.debug, Rails.logger.info, Rails.logger.warn, Rails.logger.error, and Rails.logger.fatal methods to log messages at different levels.

Example of logging a message:

Rails.logger.info "Processing request: #{request.path}"