サービス オブジェクトをPlantTree
呼び出すジョブがあります。ジョブをテストして、引数を使用してサービスをPlantTree
インスタンス化し、メソッドを呼び出すことを確認したいと思います。PlantTree
tree
call
私はサービスが何をするか、またはその結果には興味がありません。それには独自のテストがあり、仕事のためにそれらのテストを繰り返したくありません。
# app/jobs/plant_tree_job.rb
class PlantTreeJob < ActiveJob::Base
def perform(tree)
PlantTree.new(tree).call
end
end
# app/services/plant_tree.rb
class PlantTree
def initialize(tree)
@tree = tree
end
def call
# Do stuff that plants the tree
end
end
ご覧のとおり、クラスはジョブPlantTree
のメソッドにハードコーディングされています。perform
したがって、それを偽造して依存関係として渡すことはできません。perform メソッドの存続期間中、それを偽造する方法はありますか? 何かのようなもの...
class PlantTreeJobTest < ActiveJob::TestCase
setup do
@tree = create(:tree)
end
test "instantiates PlantTree service with `@tree` and calls `call`" do
# Expectation 1: PlantTree will receive `new` with `@tree`
# Expectation 2: PlatTree will receive `call`
PlantTreeJob.perform_now(@tree)
# Verify that expections 1 and 2 happened.
end
end
MiniTest を使用する Rails のデフォルト スタックを使用しています。これは Rspec で実行できることは知っていますが、MiniTest にしか興味がありません。MiniTest のみ、またはデフォルトの Rails スタックでこれを行うことができない場合は、外部ライブラリを使用することにオープンです。