W3cubDocs

/Crystal

module Spec::Methods

Defined in:

spec/methods.cr

Instance Method Summary

Instance Method Detail

def after_all(&block)Source

Executes the given block after the last spec in the current context runs.

A context is defined by #describe or #context blocks, or outside of them it's the root context. This is independent of the source location the specs and this hook are defined.

If multiple blocks are registered on the same context, they are executed in order of definition.

require "spec

it "sample_a" {}

describe "nested_context" do
  after_all do
    puts "runs at end of nested_context"
  end

  it "sample_b" {}
end

def after_each(&block)Source

Executes the given block after each spec in the current context runs.

A context is defined by #describe or #context blocks, or outside of them it's the root context. Nested contexts inherit the *_each blocks of their ancestors.

If multiple blocks are registered for the same spec, the blocks defined in the outermost context go first. Blocks on the same context are executed in order of definition.

require "spec

it "sample_a" {}

describe "nested_context" do
  after_each do
    puts "runs after sample_b"
  end

  it "sample_b" {}
end

def around_all(&block : ExampleGroup::Procsy -> )Source

Executes the given block when the current context runs.

The block must call run on the given Context::Procsy object.

This is essentially a #before_all and #after_all hook combined into one. It is useful for example when setup and teardown steps need shared state.

A context is defined by #describe or #context blocks. This hook does not work outside such a block (i.e. in the root context).

If multiple blocks are registered for the same spec, the blocks defined in the outermost context go first. Blocks on the same context are executed in order of definition.

require "spec

describe "main_context" do
  around_each do |example|
    puts "runs at beginning of main_context"
    example.run
    puts "runs at end of main_context"
  end

  it "sample_a" {}

  describe "nested_context" do
    around_each do |example|
      puts "runs at beginning of nested_context"
      example.run
      puts "runs at end of nested_context"
    end

    it "sample_b" {}
  end
end

def around_each(&block : Example::Procsy -> )Source

Executes the given block when each spec in the current context runs.

The block must call run on the given Example::Procsy object.

This is essentially a #before_each and #after_each hook combined into one. It is useful for example when setup and teardown steps need shared state.

A context is defined by #describe or #context blocks, or outside of them it's the root context. Nested contexts inherit the *_each blocks of their ancestors.

If multiple blocks are registered for the same spec, the blocks defined in the outermost context go first. Blocks on the same context are executed in order of definition.

require "spec

it "sample_a" {}

describe "nested_context" do
  around_each do |example|
    puts "runs before sample_b"
    example.run
    puts "runs after sample_b"
  end

  it "sample_b" {}
end

def before_all(&block)Source

Executes the given block before the first spec in the current context runs.

A context is defined by #describe or #context blocks, or outside of them it's the root context. This is independent of the source location the specs and this hook are defined.

If multiple blocks are registered on the same context, they are executed in order of definition.

require "spec

it "sample_a" {}

describe "nested_context" do
  before_all do
    puts "runs at start of nested_context"
  end

  it "sample_b" {}
end

def before_each(&block)Source

Executes the given block before each spec in the current context runs.

A context is defined by #describe or #context blocks, or outside of them it's the root context. Nested contexts inherit the *_each blocks of their ancestors.

If multiple blocks are registered for the same spec, the blocks defined in the outermost context go first. Blocks on the same context are executed in order of definition.

require "spec

it "sample_a" {}

describe "nested_context" do
  before_each do
    puts "runs before sample_b"
  end

  it "sample_b" {}
end

def context(description, file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &block)Source

Defines an example group that establishes a specific context, like empty array versus array with elements. Inside &block examples are defined by #it or #pending.

It is functionally equivalent to #describe.

If focus is true, only this #context, and others marked with focus: true, will run.

def describe(description, file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &block)Source

Defines an example group that describes a unit to be tested. Inside &block examples are defined by #it or #pending.

Several #describe blocks can be nested.

Example:

describe "Int32" do
  describe "+" do
    it "adds" { (1 + 1).should eq 2 }
  end
end

If focus is true, only this #describe, and others marked with focus: true, will run.

def fail(msg, file = __FILE__, line = __LINE__)Source

Fails an example.

This method can be used to manually fail an example defined in an #it block.

def it(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &block)Source

Defines a concrete test case.

The test is performed by the block supplied to &block.

Example:

it "adds" { (1 + 1).should eq 2 }

It is usually used inside a #describe or #context section.

If focus is true, only this test, and others marked with focus: true, will run.

def pending(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil, &)Source

Defines a pending test case.

&block is never evaluated. It can be used to describe behaviour that is not yet implemented.

Example:

pending "check cat" { cat.alive? }

It is usually used inside a #describe or #context section.

If focus is true, only this test, and others marked with focus: true, will run.

def pending(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, focus : Bool = false, tags : String | Enumerable(String) | Nil = nil)Source

Defines a yet-to-be-implemented pending test case

If focus is true, only this test, and others marked with focus: true, will run.

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Spec/Methods.html