September 07, 2019
Sometimes I find myself wanting to test a ruby code outside the context of
Rails. If what I want to test is trivial and so simple, then creating a
demo.rb looks sufficient. In most cases, I add unit tests so I can
debug easily and effectively. Of course, that means we’re looking for something
a little complex than our
In this article, I aim to share with you how I do this using my go-to testing framework, RSpec.
Let’s create our project directory.
$ mkdir demo_project
rspec inside demo_project.
$ cd demo_project $ touch Gemfile
Inside the Gemfile, add the ff. lines:
source 'https://rubygems.org' gem 'rspec'
$ bundle exec rspec --init
bundle exec is important here to make sure we’re running the rspec
version from our Gemfile. This command will generate a
$ touch spec/demo_spec.rb
Write the spec you want to test.
require 'spec_helper' RSpec.describe "Demo spec" do it "works" do expect(1 + 1).to eq 2 end end
bundle exec rspec spec and you should see the test pass.