LoginSignup
1
0

More than 3 years have passed since last update.

RSpec 基本構文の理解

Last updated at Posted at 2021-04-27

【RSpec 基本構文の理解】

RSpecの構文に全然慣れないので超基本から復習しました。
簡単な自分用の備忘録です。

RSpecの書き方

・テストケースを整理、分類 : describe, context
・テストコードを実行 : before, it

RSpec.describe ['テストの対象'], type: ['Specの種類'] do
  describe ['テストの対象'] do
    context ['どんな条件でテストをするか'] do
      before do
        ['事前の準備']
      end

      it ['仕様の内容 : テストで期待する正常な概要'] do
        ['テストで期待する正常な動作']
      end
    end
  end
end

itが1番肝心。
最終的にitに書かれた状態になればテストは正常におこなわれたということ。

itに到達するまでに、describeやcontextを何個もネストすることができる。
内側のdescribeやcontextやitを複数記述することもできる。

※type指定は、外側のdescribeにだけおこなうことができる。

Specを実行

rspecコマンドにSpecファイルの場所を指定して、実行する。

$ bundle exec rspec spec/system/example_spec.rb

問題無く、テストが実行されると、

1 example, 0 failures

表示される。
 → 1件のテストを実行して、失敗が0件で問題無く、テストが通った。(今回は1件のみ)

1
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
1
0