Class: Tryouts::ExpectationEvaluators::NonNil
- Defined in:
- lib/tryouts/expectation_evaluators/non_nil.rb
Overview
Evaluator for non-nil expectations using syntax: #=*>
PURPOSE:
- Validates that the test result is not nil and no exception occurred
- Provides a simple "anything goes" expectation for existence checks
- Useful for API responses, object creation, method return values
SYNTAX: #=*> Examples:
user = User.create(name: "test")
#=*> # Pass: user object exists (not nil)
response = api_call()
#=*> # Pass: got some response (not nil)
nil
#=*> # Fail: result is nil
raise StandardError.new("error")
#=*> # Fail: exception occurred
VALIDATION LOGIC:
- Passes when result is not nil AND no exception was raised during execution
- Fails when result is nil OR an exception occurred
- Does not evaluate any additional expression (unlike other expectation types)
IMPLEMENTATION DETAILS:
- Simple existence check without complex evaluation
- No expression parsing needed - syntax is just #=*>
- Expected display shows "non-nil result with no exception"
- Actual display shows the actual result value or exception
DESIGN DECISIONS:
- Uses #=*> syntax where * represents "anything"
- Part of unified #= prefix convention for all expectation types
- Complements existing boolean and equality expectations
- Provides simple alternative to complex conditional expressions
- Useful for integration tests where exact values are unpredictable
VARIABLE ACCESS:
- No special variables needed since no expression is evaluated
- Works purely on the actual test result value
Instance Attribute Summary
Attributes inherited from Base
#context, #expectation, #test_case
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Tryouts::ExpectationEvaluators::Base
Class Method Details
.handles?(expectation_type) ⇒ Boolean
52 53 54 |
# File 'lib/tryouts/expectation_evaluators/non_nil.rb', line 52 def self.handles?(expectation_type) expectation_type == :non_nil end |
Instance Method Details
#evaluate(actual_result = nil, caught_exception: nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tryouts/expectation_evaluators/non_nil.rb', line 56 def evaluate(actual_result = nil, caught_exception: nil) # Check if an exception occurred during test execution if caught_exception return build_result( passed: false, actual: "(#{caught_exception.class}) #{caught_exception.}", expected: 'non-nil result with no exception', ) end # Check if result is nil passed = !actual_result.nil? build_result( passed: passed, actual: actual_result, expected: 'non-nil result', ) rescue StandardError => ex handle_evaluation_error(ex, actual_result) end |