Class: RSpec::Sidekiq::Matchers::Base Private
- Inherits:
-
Object
- Object
- RSpec::Sidekiq::Matchers::Base
- Includes:
- Matchers::Composable, Mocks::ArgumentMatchers
- Defined in:
- lib/rspec/sidekiq/matchers/base.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Direct Known Subclasses
Instance Attribute Summary collapse
- #actual_jobs ⇒ Object readonly private
- #expected_arguments ⇒ Object readonly private
- #expected_count ⇒ Object readonly private
- #expected_options ⇒ Object readonly private
- #klass ⇒ Object readonly private
Instance Method Summary collapse
- #at(timestamp) ⇒ Object private
- #at_least(n) ⇒ Object private
- #at_most(n) ⇒ Object private
- #common_message ⇒ Object private
- #count_message ⇒ Object private
- #description ⇒ Object private
- #exactly(n) ⇒ Object private
- #failure_message ⇒ Object private
- #failure_message_when_negated ⇒ Object private
- #formatted(thing) ⇒ Object private
- #immediately ⇒ Object private
- #in(interval) ⇒ Object private
-
#initialize ⇒ Base
constructor
private
A new instance of Base.
- #never ⇒ Object private
- #normalize_arguments(args) ⇒ Object private
- #on(queue) ⇒ Object private
- #once ⇒ Object private
- #prefix_message ⇒ Object private
- #set_expected_count(relativity, n) ⇒ Object private
- #thrice ⇒ Object private
- #times ⇒ Object (also: #time) private
- #twice ⇒ Object private
- #with(*expected_arguments) ⇒ Object private
- #with_context(**kwargs) ⇒ Object private
Constructor Details
#initialize ⇒ Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Base.
186 187 188 189 190 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 186 def initialize @expected_arguments = [any_args] @expected_options = {} set_expected_count :positive, 1 end |
Instance Attribute Details
#actual_jobs ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
184 185 186 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184 def actual_jobs @actual_jobs end |
#expected_arguments ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
184 185 186 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184 def expected_arguments @expected_arguments end |
#expected_count ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
184 185 186 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184 def expected_count @expected_count end |
#expected_options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
184 185 186 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184 def @expected_options end |
#klass ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
184 185 186 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184 def klass @klass end |
Instance Method Details
#at(timestamp) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
197 198 199 200 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 197 def at() @expected_options["at"] = .to_time.to_i self end |
#at_least(n) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
242 243 244 245 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 242 def at_least(n) set_expected_count :at_least, n self end |
#at_most(n) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
247 248 249 250 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 247 def at_most(n) set_expected_count :at_most, n self end |
#common_message ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
327 328 329 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 327 def "#{} #{} #{klass} #{expected_count.last == 1 ? "job" : "jobs"}" end |
#count_message ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
335 336 337 338 339 340 341 342 343 344 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 335 def case expected_count[0] when :positive "a" when :exactly expected_count[1] else "#{expected_count[0].to_s.gsub('_', ' ')} #{expected_count[1]}" end end |
#description ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
289 290 291 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 289 def description "#{} with arguments #{expected_arguments}" end |
#exactly(n) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
237 238 239 240 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 237 def exactly(n) set_expected_count :exactly, n self end |
#failure_message ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 293 def = ["expected to #{}"] if expected_arguments << " with arguments:" << " -#{formatted(expected_arguments)}" end if .any? << " with context:" << " -#{formatted()}" end if actual_jobs.any? << "but enqueued only jobs" if expected_arguments = actual_jobs.map do |job| base = [" -JID:#{job.jid} with arguments:"] base << " -#{formatted(job.args)}" if .any? base << " with context: #{formatted(job.context)}" end base.join("\n") end << .join("\n") end else << "but enqueued 0 jobs" end .join("\n") end |
#failure_message_when_negated ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
346 347 348 349 350 351 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 346 def = ["expected not to #{} but enqueued #{actual_jobs.count}"] << " arguments: #{expected_arguments}" if expected_arguments.any? << " options: #{}" if .any? .join("\n") end |
#formatted(thing) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
353 354 355 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 353 def formatted(thing) RSpec::Support::ObjectFormatter.format(thing) end |
#immediately ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
207 208 209 210 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 207 def immediately @expected_options["at"] = nil self end |
#in(interval) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
202 203 204 205 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 202 def in(interval) @expected_options["at"] = (Time.now.to_f + interval.to_f).to_i self end |
#never ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
217 218 219 220 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 217 def never set_expected_count :exactly, 0 self end |
#normalize_arguments(args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 357 def normalize_arguments(args) if args.is_a?(Array) args.map{ |x| normalize_arguments(x) } elsif args.is_a?(Hash) args.each_with_object({}) do |(key, value), hash| hash[key.to_s] = normalize_arguments(value) end elsif args.is_a?(Symbol) args.to_s else args end end |
#on(queue) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
212 213 214 215 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 212 def on(queue) @expected_options["queue"] = queue self end |
#once ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
222 223 224 225 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 222 def once set_expected_count :exactly, 1 self end |
#prefix_message ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
331 332 333 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 331 def raise NotImplementedError end |
#set_expected_count(relativity, n) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 277 def set_expected_count(relativity, n) n = case n when Integer then n when :once then 1 when :twice then 2 when :thrice then 3 else raise ArgumentError, "Unsupported #{n} in '#{relativity} #{n}'. Use either an Integer, :once, :twice, or :thrice." end @expected_count = [relativity, n] end |
#thrice ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
232 233 234 235 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 232 def thrice set_expected_count :exactly, 3 self end |
#times ⇒ Object Also known as: time
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
252 253 254 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 252 def times self end |
#twice ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
227 228 229 230 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 227 def twice set_expected_count :exactly, 2 self end |
#with(*expected_arguments) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
192 193 194 195 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 192 def with(*expected_arguments) @expected_arguments = normalize_arguments(expected_arguments) self end |
#with_context(**kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/rspec/sidekiq/matchers/base.rb', line 257 def with_context(**kwargs) raise ArgumentError, "Must specify keyword arguments to with_context" if kwargs.empty? # gather keys and compare against currently set expected_options # Someone could have accidentally used with_context and other # chainables with different expectations. Better to explicitly # inform loudly of clashes than let them overwrite silently normalized = normalize_arguments(kwargs) already_set = normalized.keys & @expected_options.keys if already_set.any? prettied = already_set.map { |key| "'#{key}'" } raise ArgumentError, "There are already expectations against #{prettied.join(",")}. Did you already call other context chainables like `on` or `at`?" end # We're good, no accidental overwrites of expectations @expected_options.merge!(normalized) self end |