Skip to content

Instantly share code, notes, and snippets.

@Mattamorphic
Last active April 28, 2020 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mattamorphic/2e041f0808132ce8222c10c828388476 to your computer and use it in GitHub Desktop.
Save Mattamorphic/2e041f0808132ce8222c10c828388476 to your computer and use it in GitHub Desktop.
Dry-Example
# frozen_string_literal: true
require "minitest/mock"
class MyJobTest < ActiveJob::TestCase
# Helper function to wrap mocking the :mymethod method
#
# expectations - Array of Procs/Lambdas that will be checked against the message
# argument that is passed to the :mymethod method in the MyModule::myclass
# object, all must return true.
#
def mymethod_mock(expectations: [])
mock = Minitest::Mock.new
mock.expect :call, nil do |id:, message:|
ticket_id.is_a?(Integer) &&
message.is_a?(String) &&
expectations.all? do |func|
func.call(message)
end
end
MyModule::myclass.any_instance.stub(:mymethod, mock) do
yield
end
mock.verify
end
test "check job calls method" do
assert(mymethod_mock {
MyJob.perform_now(id: 1234)
})
end
test "check job creates message" do
assert(mymethod_mock(expectations:[
->(message) { message.downcase.include?("hello, world!") }
]) {
MyJob.perform_now(id: 1234)
})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment