Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Created March 11, 2021 22:18
Show Gist options
  • Save benoittgt/b4de19bd614b1473e92f8c3e45131137 to your computer and use it in GitHub Desktop.
Save benoittgt/b4de19bd614b1473e92f8c3e45131137 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# ruby ruby_trap.rb
begin
require "bundler/inline"
end
gemfile(true) do
source "https://rubygems.org"
gem "rspec", "~> 3"
end
require 'rspec/autorun'
require 'json'
class AsyncJob
def self.perform_later(message); end
end
class Logger
def self.warn(payload); end
end
class EventParser
attr_reader :payload
def initialize(payload)
@payload = payload
end
def call
AsyncJob.perform_later(message_parsed)
Logger.warn(payload_obfuscated)
end
def message_parsed
@_message_parsed ||= JSON.parse(payload)
end
def payload_obfuscated
extracted_infos = message_parsed['x']
extracted_infos['email'] = extracted_infos['email'].gsub(/[a-z]+(?=@)/, '*')
extracted_infos
end
end
RSpec.configure do |config|
config.formatter = :documentation
end
RSpec.describe 'Navigation Bar' do
let(:structured_hash) { { 'a' => 1, 'x' =>{ 'email' => '[email protected]' } } }
let(:payload) { structured_hash.to_json }
it 'calls AsyncJob' do
allow(AsyncJob).to receive(:perform_later)
EventParser.new(payload).call
expect(AsyncJob).to have_received(:perform_later).with(structured_hash)
end
end
=begin
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Using bundler 2.2.3
Using diff-lcs 1.4.4
Using rspec-support 3.10.2
Using rspec-mocks 3.10.2
Using rspec-core 3.10.1
Using rspec-expectations 3.10.1
Using rspec 3.10.0
Navigation Bar
calls AsyncJob (FAILED - 1)
Failures:
1) Navigation Bar calls AsyncJob
Failure/Error: expect(AsyncJob).to have_received(:perform_later).with(structured_hash)
#<AsyncJob (class)> received :perform_later with unexpected arguments
expected: ({"a"=>1, "x"=>{"email"=>"[email protected]"}})
got: ({"a"=>1, "x"=>{"email"=>"*@example.com"}})
Diff:
@@ -1 +1 @@
-[{"a"=>1, "x"=>{"email"=>"[email protected]"}}]
+[{"a"=>1, "x"=>{"email"=>"*@example.com"}}]
# rspec_hash_mutation.rb:61:in `block (2 levels) in <main>'
Finished in 0.03061 seconds (files took 0.09747 seconds to load)
1 example, 1 failure
Failed examples:
rspec rspec_hash_mutation.rb:56 # Navigation Bar calls AsyncJob
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment