Last active
April 28, 2020 10:06
-
-
Save Mattamorphic/2e041f0808132ce8222c10c828388476 to your computer and use it in GitHub Desktop.
Dry-Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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