0
0

RSpecを導入する手順

Posted at

はじめに

Rubyで何かを開発するにあたってRSpecを導入したい。

RSpec公式の「Let's get started!」を見たり、RDocを読んだりして導入はできるけど、メモ書き程度に導入手順を残す。

なお、RubyやBundler、gitは導入済みであるとする

手順

1. 開発用のディレクトリを作る

$ mkdir awesome_directory
$ cd awesome_directory

2. budler init

開発用のディレクトリの中でGemfileを生成する

$ bundler init
$ ls
Gemfile

3. Gemfileを編集する

# frozen_string_literal: true

source "https://rubygems.org"

gem "rspec", "~> 3.0"
% git diff
diff --git a/Gemfile b/Gemfile
index d2403f1..7afe5b1 100644
--- a/Gemfile
+++ b/Gemfile
@@ -2,4 +2,4 @@
 
 source "https://rubygems.org"
 
-# gem "rails"
+gem "rspec", "~> 3.0"

4. bundle install

$ bundle install
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Gemfile.lockができる。

$ % ls
Gemfile         Gemfile.lock

5. rspec --init

初期化をする

$ bundle exec rspec --init
create   .rspec
create   spec/spec_helper.rb

6. テストの実行を試してみる

まずは実行できるかどうかの確認のために、適当なファイルを生成する。
hoge.rbはテスト対象、spec/hoge_spec.rbはテスト用のファイル。RSpecのファイルは _spec.rbというsuffixをつけるのが慣例。

$ touch hoge.rb
$ touch spec/hoge_spec.rb

hoge.rbspec/hoge_spec.rbにコードを書く。

hoge.rb
class Hoge
  def self.hello
    puts 'Hello, Hoge!'
  end
end
spec/hoge_spec.rb
RSpec.describe Hoge do
  describe '.hello' do
    it 'prints "Hello!" to the console' do
      expect { Hoge.hello }.to output("Hello!\n").to_stdout
    end
  end
end

RSpecを実行してみる。

$ bundle exec rspec spec/hoge_spec.rb 

An error occurred while loading ./spec/hoge_spec.rb.
Failure/Error:
  RSpec.describe Hoge do
    describe '.hello' do
      it 'prints "Hello!" to the console' do
        expect { Hoge.hello }.to output("Hello!\n").to_stdout
      end
    end
  end

NameError:
  uninitialized constant Hoge
# ./spec/hoge_spec.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00004 seconds (files took 0.17748 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

Hoge classが読み込めないというエラーが発生するので、spec/spec_helper.rbもしくはspec/hoge_spec.rbで読み込むようにする。
以下は、spec/spec_helper.rbhoge.rbのみ読み込む設定の例。

spec/spec_helper.rb
(省略)

require_relative '../hoge'

RSpec.configure do |config|

(省略)

再度RSpecを実行するとテストがパスする。

$ bundle exec rspec spec/hoge_spec.rb
.

Finished in 0.00313 seconds (files took 0.15465 seconds to load)
1 example, 0 failures

念の為、テストが落ちることも確認する。

class Hoge
  def self.hello
    puts 'Bad...'
  end
end
% rspec spec/hoge_spec.rb
F

Failures:

  1) Hoge.hello prints "Hello!" to the console
     Failure/Error: expect { Hoge.hello }.to output("Hello!\n").to_stdout
     
       expected block to output "Hello!\n" to stdout, but output "Hello, Hoge!\n"
       Diff:
       @@ -1 +1 @@
       -Hello!
       +Bad...
       
     # ./spec/hoge_spec.rb:4:in `block (3 levels) in <top (required)>'

Finished in 0.01361 seconds (files took 0.21231 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/hoge_spec.rb:3 # Hoge.hello prints "Hello!" to the console

追加でやりたいこと

テストファイルごとにRSpec.describeと書かなくてもいいようにする

RSpec.configuerexpose_dsl_globallyを追加する。

spec/spec_helper.rb
(省略)

RSpec.configure do |config|
  config.expose_dsl_globally = true

(省略)

最後に

他にメモしておきたいことができたら追記します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0