0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

shared_contextとは

Posted at

Using shared_context

近くのどちらか一方の共有例のコンテキストで査定されるブロックを定義するために使うexapmple groupは,ローカルではexample groupのinclude_contextを使って、またグローバルではconfig.include_contextを使っいます

Use shared_context to define a block that will be evaluated in the context of example groups either locally, using include_context in an example group, or globally using config.include_context.

メタデータの一致によってshared contextsを暗黙的に含むとき、普通の方法は全体のグループのcontextにあるexample group上のメタデータを一致の定義することです。

When implicitly including shared contexts via matching metadata, the normal way is to define matching metadata on an example group, in which case the context is included in the entire group.

Background

Given a file named “shared_stuff.rb” with:

RSpec.configure do |rspec|
  # This config option will be enabled by default on RSpec 4,
  # but for reasons of backwards compatibility, you have to
  # set it on RSpec 3.
  #
  # It causes the host group and examples to inherit metadata
  # from the shared context.
  rspec.shared_context_metadata_behavior = :apply_to_host_groups
end

RSpec.shared_context "shared stuff", :shared_context => :metadata do
  before { @some_var = :some_value }
  def shared_method
    "it works"
  end
  let(:shared_let) { {'arbitrary' => 'object'} }
  subject do
    'this is the subject'
  end
end

RSpec.configure do |rspec|
  rspec.include_context "shared stuff", :include_shared => true
end

Declare a shared context and include it with include_context

Given a file named “sharedcontextexample.rb” with:

require "./shared_stuff.rb"

RSpec.describe "group that includes a shared context using 'include_context'" do
  include_context "shared stuff"

  it "has access to methods defined in shared context" do
    expect(shared_method).to eq("it works")
  end

  it "has access to methods defined with let in shared context" do
    expect(shared_let['arbitrary']).to eq('object')
  end

  it "runs the before hooks defined in the shared context" do
    expect(@some_var).to be(:some_value)
  end

  it "accesses the subject defined in the shared context" do
    expect(subject).to eq('this is the subject')
  end

  group = self

  it "inherits metadata from the included context" do |ex|
    expect(group.metadata).to include(:shared_context => :metadata)
    expect(ex.metadata).to include(:shared_context => :metadata)
  end
end

When I run rspec shared_context_example.rb

Then the examples should all pass.

感想

  • モジュールのようにcontextや変数を使うことができる
  • .metadataでメタデータを使って引用する
  • 実践で使ってみたいな
  • テストの方針を組み立てないと簡単に作ることが出来なさそう

メタデータを使って結局なんで使うのか忘れた

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?