class Base
Represents the base test case class. Provides core functionality for test execution including hooks for setup and teardown.
Definitions
def initialize(assertions)
Initialize a new test case instance.
Signature
-
parameter
assertionsAssertions The assertions instance used to track test results.
Implementation
def initialize(assertions)
@__assertions__ = assertions
end
def inspect
Signature
-
returns
String A string representation of the test case.
Implementation
def inspect
"\#<Sus::Base for #{self.class.description.inspect}>"
end
def before
A hook which is called before the test is executed.
If you override this method, you must call super.
Implementation
def before
end
def after(error = nil)
A hook which is called after the test is executed.
If you override this method, you must call super.
Implementation
def after(error = nil)
end
def around(&block)
Wrap logic around the test being executed.
Invokes the before hook, then the block, then the after hook.
Signature
-
yields
{...} the block which should execute a test.
Implementation
def around(&block)
self.before
return block.call
rescue => error
raise
ensure
self.after(error)
end
def assert(...)
Make an assertion about a condition.
Signature
-
parameter
conditionBoolean The condition to assert.
-
parameter
messageString | Nil Optional message describing the assertion.
Implementation
def assert(...)
@__assertions__.assert(...)
end
def inform(...)
Print an informational message during test execution.
Signature
-
parameter
messageString | Nil The message to print, or a block that returns a message.
Implementation
def inform(...)
@__assertions__.inform(...)
end
def be(*arguments)
Create a Be predicate matcher.
Signature
-
parameter
argumentsArray Optional method name and arguments to call on the subject.
-
returns
Be, Class A Be predicate if arguments are provided, otherwise the Be class.
Implementation
def be(*arguments)
if arguments.any?
Be.new(*arguments)
else
Be
end
end
def be_a(klass)
Create a predicate that checks if the subject is an instance of a class.
Signature
-
parameter
klassClass The class to check against.
-
returns
Be A new Be predicate.
Implementation
def be_a(klass)
Be.new(:is_a?, klass)
end
def be_nil
Create a predicate that checks if the subject is nil.
Signature
-
returns
Be A Be predicate that checks for nil.
Implementation
def be_nil
Be::NIL
end
def be_equal(other)
Create a predicate that checks object identity equality.
Signature
-
parameter
otherObject The object to compare against.
-
returns
Be A new Be predicate.
Implementation
def be_equal(other)
Be.new(:equal?, other)
end
def be_truthy
Create a predicate that checks if the subject is truthy.
Signature
-
returns
BeTruthy A BeTruthy predicate.
Implementation
def be_truthy
BeTruthy
end
def be_falsey
Create a predicate that checks if the subject is falsey.
Signature
-
returns
BeFalsey A BeFalsey predicate.
Implementation
def be_falsey
BeFalsey
end
def be_within(value)
Create a predicate that checks if the subject is within a tolerance or range.
Signature
-
parameter
valueNumeric, Range The tolerance value or range to check against.
-
returns
BeWithin, BeWithin::Bounded A BeWithin predicate.
Implementation
def be_within(value)
case value
when Range
BeWithin::Bounded.new(value)
else
BeWithin.new(value)
end
end
def expect(subject = nil, &block)
Create an expectation about a subject or block.
Signature
-
parameter
subjectObject, nil The subject to make expectations about.
-
yields
{...} Optional block to make expectations about.
-
returns
Expect A new Expect instance.
Implementation
def expect(subject = nil, &block)
if block_given?
Expect.new(@__assertions__, block, distinct: true)
else
Expect.new(@__assertions__, subject, distinct: true)
end
end
def have(*predicates)
Create a predicate that checks if the subject has all of the given predicates.
Signature
-
parameter
predicatesArray The predicates to check.
-
returns
Have::All A Have::All predicate.
Implementation
def have(*predicates)
Have::All.new(predicates)
end
def have_keys(*keys)
Create a predicate that checks if the subject (hash) has the specified keys.
Signature
-
parameter
keysArray Keys to check for. Can be symbols/strings or hashes with key-predicate pairs.
-
returns
Have::All A Have::All predicate.
Implementation
def have_keys(*keys)
predicates = []
keys.each do |key|
if key.is_a?(Hash)
key.each do |key, predicate|
predicates << Have::Key.new(key, predicate)
end
else
predicates << Have::Key.new(key)
end
end
Have::All.new(predicates)
end
def have_attributes(**attributes)
Create a predicate that checks if the subject has the specified attributes with matching values.
Signature
-
parameter
attributesHash A hash of attribute names to predicates.
-
returns
Have::All A Have::All predicate.
Implementation
def have_attributes(**attributes)
predicates = attributes.map do |key, value|
Have::Attribute.new(key, value)
end
Have::All.new(predicates)
end
def have_any(*predicates)
Create a predicate that checks if the subject matches any of the given predicates.
Signature
-
parameter
predicatesArray The predicates to check.
-
returns
Have::Any A Have::Any predicate.
Implementation
def have_any(*predicates)
Have::Any.new(predicates)
end
def have_value(predicate)
Create a predicate that checks if the subject (collection) has any value matching the predicate.
Signature
-
parameter
predicateObject The predicate to apply to each value.
-
returns
Have::Any A Have::Any predicate.
Implementation
def have_value(predicate)
Have::Any.new([Have::Value.new(predicate)])
end
def have_duration(...)
Create a predicate that measures the duration of a block execution.
Signature
-
parameter
predicateObject The predicate to apply to the measured duration.
-
returns
HaveDuration A new HaveDuration predicate.
Implementation
def have_duration(...)
HaveDuration.new(...)
end
def skip(reason)
Skip the current test with a reason.
Signature
-
parameter
reasonString The reason for skipping the test.
Implementation
def skip(reason)
@__assertions__.skip(reason)
throw :skip, reason
end
def skip_unless_method_defined(method, target)
Skip the test unless a method is defined on the target.
Signature
-
parameter
methodSymbol The method name to check.
-
parameter
targetModule, Class The target class or module to check.
Implementation
def skip_unless_method_defined(method, target)
unless target.method_defined?(method)
skip "Method #{method} is not defined in #{target}!"
end
end
def skip_unless_constant_defined(constant, target = Object)
Skip the test unless a constant is defined.
Signature
-
parameter
constantSymbol, String The constant name to check.
-
parameter
targetModule, Class The target class or module to check.
Implementation
def skip_unless_constant_defined(constant, target = Object)
unless target.const_defined?(constant)
skip "Constant #{constant} is not defined in #{target}!"
end
end
def skip_unless_minimum_ruby_version(version, ruby_version = RUBY_VERSION)
Skip the test unless the Ruby version meets the minimum requirement.
Signature
-
parameter
versionString The minimum Ruby version required.
-
parameter
ruby_versionString The Ruby version to check.
Implementation
def skip_unless_minimum_ruby_version(version, ruby_version = RUBY_VERSION)
unless compare_ruby_version(ruby_version, version) >= 0
skip "Ruby #{version} is required, but running #{ruby_version}!"
end
end
def skip_if_maximum_ruby_version(version, ruby_version = RUBY_VERSION)
Skip the test if the Ruby version exceeds the maximum supported version.
Signature
-
parameter
versionString The maximum Ruby version supported.
-
parameter
ruby_versionString The Ruby version to check.
Implementation
def skip_if_maximum_ruby_version(version, ruby_version = RUBY_VERSION)
if compare_ruby_version(ruby_version, version) >= 0
skip "Ruby #{version} is not supported, but running #{ruby_version}!"
end
end
def skip_if_ruby_platform(pattern)
Skip the test if the Ruby platform matches the pattern.
Signature
-
parameter
patternRegexp The platform pattern to match against.
Implementation
def skip_if_ruby_platform(pattern)
if match = RUBY_PLATFORM.match(pattern)
skip "Ruby platform #{match} is not supported!"
end
end
def mock(target, &block)
Create or access a mock for the given target.
Signature
-
parameter
targetObject The object to mock.
-
yields
{|mock| ...} Optional block to configure the mock.
-
returns
Mock The mock instance for the target.
Implementation
def mock(target, &block)
# Pull in the extra functionality:
self.singleton_class.prepend(Mocks)
# Redirect the method to the new functionality:
self.mock(target, &block)
end
def raise_exception(...)
Create a predicate that checks if a block raises an exception.
Signature
-
parameter
exception_classClass The exception class to expect.
-
parameter
messageString | Regexp | Object | Nil Optional message matcher.
-
returns
RaiseException A new RaiseException predicate.
Implementation
def raise_exception(...)
RaiseException.new(...)
end
def receive(method, &block)
Create an expectation that a method will be called.
Signature
-
parameter
methodSymbol The method name to expect.
-
yields
{...} Optional block that returns the value to return from the method.
-
returns
Receive A new Receive expectation.
Implementation
def receive(method, &block)
Receive.new(self, method, &block)
end
def respond_to(method)
Create a predicate that checks if the subject responds to a method.
Signature
-
parameter
methodSymbol, String The method name to check for.
-
returns
RespondTo A new RespondTo predicate.
Implementation
def respond_to(method)
RespondTo.new(method)
end