Hello everyone! Can we unit test use the Automatio...
# automation-api
b
Hello everyone! Can we unit test use the Automation API? How can we apply a mocking mechanism also using the Automation API?
l
Do you mean, unit test things like
stack.up()
?
up()
isn't a unit of work and isn't appropriate for unit testing. You could try integration or full end-to-end testing instead?
If you want unit testing, you'd need to decompose your code into much smaller units.
b
Yes, you are right, but if there’s a unit that controls the execution of an
up()
, think, for example, a kind of deployer if a hypothetic
deploy()
function internally makes use of the
stack.up()
, what I’d like to do is to mock the internal automation stuff and, test the unit, read the
deploy()
function. What I tried was to mock the
inline builder function
to mimic the error case and a success case (regardless of a real Pulumi program because the unit is not the Pulumi program but the function using the Pulumi automation). To do that, I used: 1. error case - a builder function returning a function throwing an exception 2. success case - a builder function returning a function with any logic inside using this approach is the only way I figured out how to have a unit test of a unit using the Automation API
l
Sounds like you want to inject the automation-api object into the code you want to test. Then you can write your own fake implementation of it. Mocking (in Fowler's definition of it) is the worst option to solve this problem, since you don't care what Pulumi does or how it does it (and mocking is intended to prove that a white box is working in the ways it's expected to). You just want to return "success" or "failure" and test that your code it handling those results correctly, is that right?
If it is, then you need to decouple your code from Pulumi as much as is possible. Which means DI, or at least passing the Pulumi automation-api object around.
My recommendation is not to test this level of code. It's generally so well covered by the integration tests, and the 3rd party's (Pulumi's) own tests, that the benefit you get from the tests is effectively zero.