Class: Sidekiq::Sorbet::RSpec::Matchers::RejectArgs

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/sorbet/rspec/matchers.rb

Overview

Matcher for validating that a worker rejects invalid arguments

Examples:

expect(MyWorker).to reject_args(user_id: "not an integer")
expect(MyWorker).to reject_args(user_id: "bad").with_error(Sidekiq::Sorbet::InvalidArgsError)
expect(MyWorker).to reject_args(user_id: "bad").with_error(Sidekiq::Sorbet::InvalidArgsError, /Invalid/)

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ RejectArgs

Returns a new instance of RejectArgs.



219
220
221
222
223
# File 'lib/sidekiq/sorbet/rspec/matchers.rb', line 219

def initialize(args)
  @args = args
  @expected_error = nil
  @expected_message = nil
end

Instance Method Details

#descriptionObject



272
273
274
275
276
277
# File 'lib/sidekiq/sorbet/rspec/matchers.rb', line 272

def description
  desc = "reject arguments #{@args.inspect}"
  desc += " with #{@expected_error}" if @expected_error
  desc += " matching #{@expected_message.inspect}" if @expected_message
  desc
end

#failure_messageObject



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/sidekiq/sorbet/rspec/matchers.rb', line 254

def failure_message
  if @accepted
    "expected #{@actual} to reject arguments #{@args.inspect}, but it accepted them"
  elsif @expected_error && @raised_error && !@raised_error.is_a?(@expected_error)
    "expected #{@actual} to raise #{@expected_error}, but raised #{@raised_error.class}"
  elsif @expected_message && @raised_error
    "expected error message to match #{@expected_message.inspect}, " \
      "but was #{@raised_error.message.inspect}"
  else
    "unexpected state in reject_args matcher"
  end
end

#failure_message_when_negatedObject



267
268
269
270
# File 'lib/sidekiq/sorbet/rspec/matchers.rb', line 267

def failure_message_when_negated
  "expected #{@actual} not to reject arguments #{@args.inspect}, " \
    "but it raised #{@raised_error.class}"
end

#matches?(worker_class) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/sidekiq/sorbet/rspec/matchers.rb', line 236

def matches?(worker_class)
  @actual = worker_class

  begin
    worker_class.send(:build_args, **@args)
    @accepted = true
    false
  rescue StandardError => e
    @raised_error = e

    return false if @expected_error && !e.is_a?(@expected_error)

    return false if @expected_message && @raised_error.message !~ @expected_message

    true
  end
end

#with_error(error_class, message_pattern = nil) ⇒ RejectArgs

Chain to specify the expected error class and optional message pattern

Parameters:

  • error_class (Class)

    The expected error class

  • message_pattern (Regexp, nil) (defaults to: nil)

    Optional pattern to match error message

Returns:



230
231
232
233
234
# File 'lib/sidekiq/sorbet/rspec/matchers.rb', line 230

def with_error(error_class, message_pattern = nil)
  @expected_error = error_class
  @expected_message = message_pattern
  self
end