概要
rspec-core shared examples
詳細
shared examplesは複数のexample間で共有するexampleを定義できます。
include_examplesで呼び出します。
alias
main | alias |
---|---|
shared_examples | shared_context |
shared_examples | shared_examples_for |
include_examples | include_context |
shared_examples,shared_context,shared_examples_forのコード
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/shared_example_group.rb
include_examples,include_contextのコード
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb
サンプルコード(プロダクトコード)
# encoding: utf-8 class Hoge def hoge1 "hoge" end def hoge2 "hoge" end def hige1 "hige" end def hige2 "hige" end end
サンプルコード(テストコード)
# encoding: utf-8 require "spec_helper" require "hoge" describe Hoge do shared_examples "hoge" do |instance, method| it "is return hoge" do expect(instance.send(method)).to eq("hoge") end end shared_context "hige" do |instance, method| it "is return hige" do expect(instance.send(method)).to eq("hige") end end context do include_examples "hoge", Hoge.new, "hoge1" end context do include_examples "hoge", Hoge.new, "hoge2" end context do include_context "hige", Hoge.new, "hige1" end context do include_context "hige", Hoge.new, "hige2" end end