1

I'm trying to run a unit test which mocks the AWS stepfunction client. The initial function runs the following code:

client.start_execution(
        stateMachineArn=config.step_function_arn,
        input=json.dumps(
            dict(
                date=date, file=file
            )
        )
    )

And the test looks like this:

from moto import mock_stepfunctions

@mock_stepfunctions
def test_lambda_handler():
    function()

I'm not asserting anything yet but when running the test I'm getting the following error:

botocore.errorfactory.StateMachineDoesNotExist: An error occurred (StateMachineDoesNotExist) when calling the StartExecution operation: State Machine Does Not Exist: 'arn:aws:states:ca-central-1:111122223333:stateMachine:test-StateMachine'

I know the test is not communicating with the AWS server, cause that would be a different error. I provided a mock step_function_arn like this "arn:aws:states:ca-central-1:111122223333:stateMachine:test-StateMachine", but I dont think that would matter because if it's really mocked by moto the step_function arn and state machine name wouldn't matter.

So I wonder if my mock is wrong or the setup of my function is wrong.

Alex
  • 389
  • 4
  • 21

1 Answers1

2

Moto is not a typical mock-library, in the sense that you cannot just provide any input. The idea behind Moto is that it should act more like an offline version of AWS (within reason of course).

Because of this, you would have to create the state machine first, as Moto mimicks AWS' behaviour by throwing an exception if it does not exist.

Edit: If you do not want to create the stepfunction, and/or want to keep the mock simple, you can patch boto3 without Moto, as explained in this answer here: Mocking boto3 S3 client method Python

Bert Blommers
  • 1,788
  • 2
  • 13
  • 19
  • So you mean that this error comes from the fact that the provided arn is a fake one? If yes, does this mean my unit test is actually talking to the server here? Thanks, I'll test your suggestion :) – Alex Oct 24 '22 at 13:17
  • I'm assuming your test is talking to Moto - but because Moto and AWS throw the same error, it may be hard to tell. If the call takes more than 1 sec, it definitely talks to AWS. :) – Bert Blommers Oct 24 '22 at 14:34