Testing in Ruby development

Ruby also has its built-in unit testing library called Test::Unit, but most people like using Behavior-Driven Development (BDD) when using Ruby, and Rails especially. There are two popular BBD frameworks: RSpec and Cucumber.

Quote from the RSpec.info website:

"RSpec is a testing tool for the Ruby programming language. Born under the banner of Behavior-Driven Development, it is designed to make Test-Driven Development a productive and enjoyable experience."

Quote from the Cucumber repository:

"Cucumber is a tool that executes plain-text functional descriptions as automated tests."

We are lucky that there is a single package for Sublime that supports the three testing frameworks: Test::Unit, RSpec, and Cucumber. It's called RubyTest and can be found on https://github.com/maltize/sublime-text-2-ruby-tests.

Tip

The package is called sublime-text-2-ruby-tests but it supports both Sublime Text 2 and 3.

Using RubyTest for Sublime

To install the RubyTest package, we'll use Package Control. Let's open the command palette by pressing Ctrl + Shift + P in Windows or Linux and Command + Shift + P in OS X. Choose Install Package and then install the RubyTest package. The RubyTest package doesn't include any snippets but has some useful commands instead. Here is a list of all the commands and their shortcuts:

RubyTest command

Windows/Linux

OS X

Run single test

Ctrl + Shift + R

Command + Shift + R

Run all tests from current file

Ctrl + Shift + T

Command + Shift + T

Run last test(s)

Ctrl + Shift + E

Command + Shift + E

Show test panel

Ctrl + Shift + X

Command + Shift + X

Check RB, ERB Syntax

Alt + Shift + V

Option + Shift + V

Here is how a testing result should look after being run using RubyTest:

Using RubyTest for Sublime

We can see that we received 2 assertions, 1 failure, and 0 errors.

Supporting bundler

RubyTest also has a bundler autodetect feature that is based on the presence of a Gemfile in the project root directory. If this feature is enabled, RubyTest will automatically scan for the Gemfile and will add a prefix of bundle exec to any command it runs.

To enable this feature, we'll need to add a line to the RubyTest settings. We'll do it by going to Preferences | Package Settings | RubyTest | Settings – User and adding the following code:

{
   "check_for_bundler": true
}

After saving, the bundler autodetect feature is enabled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset