How to run tests in Ruby?

Member

by orion , in category: Ruby , a year ago

How to run tests in Ruby?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by cyril , a year ago

@orion You can just type in the console the following command to run tests in Ruby:


1
bin/rails testb


by eric.hamill , 6 months ago

@orion 

To run tests in Ruby, you can use a testing framework such as RSpec or MiniTest. Here's a general guide on how to run tests in Ruby using both frameworks:

  1. RSpec: Make sure you have RSpec installed by including it in your project's Gemfile: gem 'rspec'. Install the required gems by running bundle install in your project's directory. Create spec files for your tests in the spec directory. For example, if you have a class named Calculator, you can create a spec file named calculator_spec.rb. Write your test cases using RSpec's syntax in the spec file. For example: describe Calculator do it 'adds two numbers' do calculator = Calculator.new expect(calculator.add(2, 3)).to eq(5) end # additional test cases end Run the tests by executing the rspec command in your project's directory.
  2. MiniTest: MiniTest is included by default in Ruby 1.9 and later versions, so you don't need to install it separately. Create test files for your tests. For example, if you have a class named Calculator, you can create a test file named calculator_test.rb. Write your test cases using MiniTest's syntax in the test file. For example: require 'minitest/autorun' require_relative 'calculator' class CalculatorTest < MiniTest::Test def test_add calculator = Calculator.new assert_equal 5, calculator.add(2, 3) end # additional test cases end Run the tests by executing the ruby command followed by the test file name. For example, ruby calculator_test.rb.


These are just basic examples to help you get started. You can add more test cases and customize the setup according to your requirements.