Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

RSpec

RSpec のテストダブルで呼び出しているクラスメソッドの変更を検出する方法について

RSpec の class_double で呼び出しているクラスやメソッドの変更を検出する方法についてまとめます。

RSpec のテストダブルで呼び出しているクラスやメソッドの変更を検出する方法について

RSpec のテストダブルで呼び出しているクラスやメソッドの変更を検出する方法についてまとめます。

Sandi Metz 氏に学ぶ自動テストの実装分類。どれをテストする?どれをテストしない?どれをモックする?

自動テストを書くとき、 どのコードをテストすべきか? どのコードをテストすべきではないか? どのコードに対するテストをモックすべきか? について迷ったことはありますか? このようなケースに関する判断指針を Sandi Metz 氏 がまとめてくれています。

レガシーコードのリファクタリングの痛みを減らすパラメタライズドテストの活用について

この世界には二種類のソフトウェア開発者しかいない。 テストを書いてからリファクタするソフトウェア開発者と、テストを書かずにリファクタするソフトウェア開発者だ。 さらに、テストを書いてからリファクタするソフトウェア開発者には二種類のソフトウェ…

rubocop-rspecのMessageExpectation CopとGiven/When/Then

rubocop-rspec には MessageExpectation という Cop(Check項目)があります。 これはデフォルトで allow(foo).to receive(:bar) を推奨し expect(foo).to receive(:bar) に対して警告を出します。 この警告がでたときに、「ん?」どういうことなんだろう? と…

RSpecやMochaのテストにタグをつけてテスト対象をフィルタする

t_wadaさんに技術相談をしました という記事で言及されていた テストのタグ付について気になったので、具体的な実装レベルのところについてまとめます。 cureapp-dev.hatenablog.com

Circle CI のテストで RSpec のテストレポートを生成する

Ruby + RSpec のプロジェクトでテスト結果をCircle CIで取り扱いたい場合、 rspec_junit_formatter を使います。 単にこの gem を追加しておけばよしなに出力してくれます。 ※この情報は個人メモ的な内容であり、既出情報です。

RSpec | rspec config | default_path

概要 rspec config | default_path 詳細 RSpecはデフォルトでspec配下のファイルをテスト対象とします。 これを変更する場合はdefault_pathオプションを利用します。・rspecコマンド呼び出し時に指定する ・.rspecファイルに設定するなどの方法があります。 …

RSpec | rspec config | custom settings

概要 rspec config | custom settings 詳細 RSpec.configureのadd_settingメソッドを利用することで 独自パラメータを好きな名前で追加できます。 サンプル仕様 設定にdebugを追加。デフォルトをfalseにします。 実行時にはdebugモードをrandによってランダ…

RSpec | rspec config | alias_example_to

概要 rspec config | alias_example_to 詳細 RSpec.configureのalias_example_toを利用すると it(example, specify)やdescribe(context)にエイリアスを追加できます。利用目的としては・単純に好みの名前を付ける ・特定のオプションと併せて定義できる点を…

RSpec | rspec metadata | user define metadata

概要 rspec metadata | user define metadata 詳細 context, itに任意の内容のキーと値を設定すると example.metadata[:key]で、該当の設定値を取得できます。ネストして定義した場合は、オーバーライドすることになります。 hige_spec.rb require 'spec_hel…

RSpec | rspec metadata | described_class

概要 rspec metadata | described_class 詳細 described_class変数でトップレベルのdescribeにクラスが設定されていた場合、 該当クラスを取得できます。 hige_spec.rb require 'spec_helper' require 'hige' describe Hige do it "hige1" do puts described…

RSpec | rspec helper | define helper method int module

概要 rspec helper | define helper method int module 詳細 Helperをmoduleで作成し、RSpec.configureに設定することで 複数のspec間で流用することができます。 lower.rb module Lower def lower(text) text.downcase end end spec_helper.rb require 'low…

RSpec | rspec hook | before/after

概要 rspec hook | before/after 詳細 rspecのhookについて。 before/afterフックは各exampleの処理前後に処理を追加することができます。 引数に「:each」「:all」のどちらかを指定することで1回だけ実行するか、 各exampleごとに実行するか設定できます。…

RSpec | rspec command | テスト順のランダム化

概要 rspec command | テスト順のランダム化 詳細 rspec command実行時に、spec_helperに config.order = 'random' の設定を行うことで各テストケースをランダムな順序で実行します。 この設定を行った場合、テスト実行終了時に乱数の種(seed)が表示されま…

RSpec | rspec helper | let

概要 rspec helper | let 詳細 letはexample内で変数を利用したい場合に使います。 beforeで@xxxなどで変数を利用する場合との違いは、lazy loadであることです。 subject { インスタンス } 構成 $ tree ┣ lib | ┗ hige.rb ┗ spec ┣ hige_spec.rb ┗ spec_he…

RSpec | rspec | 明示のsubject

概要 rspec | 明示のsubject 詳細 明示的に初期化する場合は下記の構文を利用します。 subject { インスタンス } 構成 $ tree ┣ lib | ┗ hige.rb ┗ spec ┣ hige_spec.rb ┗ spec_helper.rb hige.rb # encoding: utf-8 class Hige attr_accessor :msg def ini…

RSpec | rspec | 暗黙のsubject

概要 rspec | 暗黙のsubject 詳細 rspec実行時にExample内でsubjectを利用できます。 subjectはデフォルトではトップ階層のdescriptionのインスタンスです。 (descriptionがネストしている場合でもトップ階層のインスタンスが設定されます) descriptionに…

RSpec | rspec command | pattern option

概要 rspec command | pattern option 詳細 rspec commandのpattern optionでワイルドカードなどを利用したパターン文字列で テスト対象を抽出して実行できます。 デフォルトは「spec/**/*_spec.rb」になっています。 構成 $ tree ┣ lib | ┣ hage.rb | ┣ h…

RSpec | rspec command | tag option

概要 rspec command | tag option 詳細 rspec commandのtag optionで行数指定で実行します。 サンプルテストコード(hige_spec.rb) # encoding: utf-8 require "spec_helper" require "hige" describe Hige do it "hige1 1", :hige1 => "hige", hige: true do…

RSpec | rspec command | line_number option

概要 rspec command | line_number option 詳細 rspec commandのline_number optionで行数指定で実行します。 サンプルテストコード(hoge_spec.rb) # encoding: utf-8 require "spec_helper" require "hoge" describe Hige do it "hoge1 1" do # some spec e…

RSpec | rspec command | init option

概要 rspec command | init option 詳細 rspec commandのinit optionで 前提 RSpec 2.14.7で動作確認 動作確認 rspec --init か rspec -i コマンドで 「.rspec」「spec」「spec/spec_helper.rb」を生成してくれます。 $rspec -i create spec/spec_helper.rb …

RSpec | rspec command | format option

概要 rspec command | format option 詳細 rspec commandのformat optionで出力フォーマットを制御できます。 サンプルテストコード(hoge_spec.rb) # encoding: utf-8 require "spec_helper" require "hoge" describe Hige do it "hoge1 1" do # some spec e…

RSpec | rspec command | fail fast option

概要 rspec command | fail fast option 詳細 rspec commandのfail fast optionでテスト失敗時に即実行を終了できます。 rspec --fail-fast サンプルコード(hoge.rb) 100メソッド定義 # encoding: utf-8 class Hoge (1..100).each do |i| define_method "hog…

RSpec | rspec command | exit status

概要 rspec command | exit status 詳細 rspec commandによるテスト実行時に 全テストが成功ならexit statusは0 そうでなければexit statusは1 になります。 サンプルコード1(hoge_spec.rb) # encoding: utf-8 require "spec_helper" require "hoge" descr…

RSpec | rspec command | example option とRSpecPiccoloの組み合わせ

概要 example option とRSpecPiccoloの組み合わせ 詳細 example option とRSpecPiccoloの組み合わせについて。 RSpecPiccoloは私が自作したgemで、パラメタライズドテストに利用します。https://rubygems.org/gems/rspec_piccolo https://github.com/tbpgr/r…

RSpec | rspec command | example option

概要 rspec command | example option 詳細 rspec commandのexample optionで ・特定の一つのexampleのみ実行 ・特定の名前に一致するexampleを全て実行 ・example optionを複数回指定して複雑な条件で任意のテストを実行する などのようなことができます。 …

RSpec | rspec-core shared examples

概要 rspec-core shared examples 詳細 shared examplesは複数のexample間で共有するexampleを定義できます。 include_examplesで呼び出します。 alias main alias shared_examples shared_context shared_examples shared_examples_for include_examples in…

RSpec | rspec-core metadata

概要 rspec-core shared metadata 詳細 metadata RSpecのexample.metadataはメタデータをハッシュで保持している。 described_class described_classでdescribeのクラスを取得可能。 shared_examlesなどを作成する際に、 処理対象のクラス名称を問わずに利用…

RSpec | rspec-core describe/context/it/example/specify

概要 rspec-core describe/context/it/example/specify 詳細 describe describeはExampleGroupを生成します。 it describeの内側で、itメソッドを利用してexampleを作成できます。 alias main alias memo describe context トップレベルではdescribeのみ利用…