I'm loving all the object oriented beauty of Trailblazer!
I have an operation that interacts with a gem (called cpanel_deployer
) to do something externally on the web. (It adds an addon domain to a cpanel.)
class Website::Deploy < Trailblazer::Operation
attr_reader :website, :cpanel
def process(params)
real_cpanel_add_domain
website.cpanel = cpanel
website.save
end
private
def setup!(params)
@cpanel = Cpanel.find(params[:cpanel_id])
@website = website.find(params[:website_id])
end
def real_cpanel_add_domain
cp_domain = CpanelDeployer::Domain.new(website.domain)
cp_panel = CpanelDeployer::Panel.new(cpanel.host, cpanel.username, cpanel.password)
res = cp_panel.add_domain(cp_domain)
raise StandardError unless res
end
end
The cpanel_deloyer
gem is already tested, so I don't need to retest it's functionality here. But in order to test the operation, I want to make sure CpanelDeployer::Panel#add_domain
is called with correct args. So I'm thinking I should mock CpanelDeployer::Panel
.
I believe it's bad practice to try to use any_instance_of
. According to thoughtbot, it's usually considered code smell... They recommend using dependency injection. Is there a good way to use dependency injection within a trailblazer operation? Is there another best practice for this kind of situation?
Aucun commentaire:
Enregistrer un commentaire