module Spectator::DSL::Expectations
Overview
Methods and macros for asserting that conditions are met.
Direct including types
Defined in:
spectator/dsl/expectations.crspectator/should.cr
Macro Summary
-
expect(actual)
Starts an expectation.
-
expect(&block)
Starts an expectation.
-
is(expected)
Short-hand form of
#is_expected
that can be used for one-liner syntax. -
is_expected
Short-hand for expecting something of the subject.
-
is_not(expected)
Short-hand, negated form of
#is_expected
that can be used for one-liner syntax. - should(*args)
- should_eventually(*args)
- should_never(*args)
- should_not(*args)
Instance Method Summary
-
#aggregate_failures(label = nil, &)
Captures multiple possible failures.
-
#fail(message = "Example failed", *, _file = __FILE__, _line = __LINE__)
Immediately fail the current test.
-
#pending(message = PendingResult::DEFAULT_REASON, *, _file = __FILE__, _line = __LINE__)
Mark the current test as pending and immediately abort.
-
#skip(message = PendingResult::DEFAULT_REASON, *, _file = __FILE__, _line = __LINE__)
Mark the current test as skipped and immediately abort.
Macro Detail
Starts an expectation.
This should be followed up with Assertion::Target#to
or Assertion::Target#to_not
.
The value passed in will be checked to see if it satisfies the conditions of the specified matcher.
This macro should be used like so:
expect(actual).to eq(expected)
Where the actual value is returned by the system under test, and the expected value is what the actual value should be to satisfy the condition.
Starts an expectation.
This should be followed up with Assertion::Target#to
or Assertion::Target#not_to
.
The value passed in will be checked to see if it satisfies the conditions of the specified matcher.
This macro should be used like so:
expect { raise "foo" }.to raise_error
The block of code is passed along for validation to the matchers.
The short, one argument syntax used for passing methods to blocks can be used. So instead of doing this:
expect(subject.size).to eq(5)
The following syntax can be used instead:
expect(&.size).to eq(5)
The method passed will always be evaluated on the subject.
TECHNICAL NOTE: This macro uses an ugly hack to detect the short-hand syntax.
The Crystal compiler will translate:
&.foo
effectively to:
{ |__arg0| __arg0.foo }
Short-hand form of #is_expected
that can be used for one-liner syntax.
For instance:
it "is 42" do
expect(subject).to eq(42)
end
Can be shortened to:
it { is(42) }
These three are functionally equivalent:
expect(subject).to eq("foo")
is_expected.to eq("foo")
is("foo")
See also: #is_not
Short-hand for expecting something of the subject.
These two are functionally equivalent:
expect(subject).to eq("foo")
is_expected.to eq("foo")
Short-hand, negated form of #is_expected
that can be used for one-liner syntax.
For instance:
it "is not 42" do
expect(subject).to_not eq(42)
end
Can be shortened to:
it { is_not(42) }
These three are functionally equivalent:
expect(subject).not_to eq("foo")
is_expected.not_to eq("foo")
is_not("foo")
See also: #is
Instance Method Detail
Captures multiple possible failures. Aborts after the block completes if there were any failed expectations in the block.
aggregate_failures do
expect(true).to be_false
expect(false).to be_true
end
Immediately fail the current test. A reason can be specified with message.
Mark the current test as pending and immediately abort. A reason can be specified with message.
Mark the current test as skipped and immediately abort. A reason can be specified with message.