Dynamic behavior in advanced mocks (expectations)?

The Expectations based advanced mock scripts don’t seem to work for me. As in, they work, but I found it hard to add dynamic behavior to them. For example, can I get the current date/time in a specific format in there? So I’m not trying to fake the date, I just want “now”.

If you want to get the current time or you can check the help documentation, it will be helpful to you:

Yes, I’ve read, but nowhere does it tell you how getting the current datetime in a specific format (basically moment.format()) works in Advanced Mock responses.

But how do I call these functions from within the Advanced Mocks, not the scripts, but the Expectations one.

I cannot even seem to use variables in that with the {{ var }} syntax.

These are also just returned verbatim, i.e.

{
    "data": [
        {
            "version": "2.1.1",
            "url": "/versions/2.1.1"
        },
        {
            "version": "2.2.1",
            "url": "/versions/2.2.1"
        }
    ],
    "status_code": 1000,
    "timestamp": "{{ Random.now('minute') }}"
}

And I’d like to add the current baseUrl to the two url’s in there but I’m not sure how to.

I even tried the Script tab, but there I have a similar issue in that I cannot get to environment variables, see Discord

Environment variables are used by the client to send requests. Mock Server is a Server and has nothing to do with the environment. So it cannot fetch the client’s environment variables.

Mock Script can use standard JavaScript syntax. For example, suppose you want to return a timestamp:




Evans thanks! Unfortunately, I’m a bit lost then. I’m able to use the script functionality to generate timestamps, that’s great, but I cannot access environment variables in it. It’s a pretty normal feature of REST APIs to return URLs to other endpoints, but it seems like the mocks do not support this then?

Yes, we currently do not support this feature

Alan hmm, that’s a shame. Maybe one bit of feedback in general, if I may be so free. A lot of functionality in ApiDog depends on external libraries that behave differently, i.e. JSON generators that can generate data that is not correctly validated by the validator. Some parts of the app not having access to other parts of the app (like mock scripts not having access to environments). Different libraries and ways of writing code in post processors vs mock scripts. All of these make sense technically, but as a user not knowing the internals of the app this causes a lot of confusion and incorrect expectations. I understand that rewriting all of this to be consistent is a ridiculously large undertaking and I do not expect you to (although it would be nice haha). But I think being very clear about the differences in the documentation would be a major improvement to the user experience. And I don’t mean implicitly describing how stuff works on one page and describing how other stuff works on the other place, but being very, very explicit about the differences. Making clear what is NOT possible etc. (the docs usually are in the style of describing that X is possible, but as a user I want to do Y, have done Y in another place of the app and then struggle to make it work in that new place). Hope that is helpful! Please reach out if you want more info :-). Always happy to provide feedback.

Thank you so much for taking the time to advise us, I can’t thank you enough

Thanks for building a cool app! :heart: (And apologies for the many support requests but i’d figured it’d be useful for future reader.)