Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Create constants to replace script variables (fake vars?) #742

Open
jmrplens opened this issue Jun 22, 2023 · 1 comment
Open
Labels

Comments

@jmrplens
Copy link

Hi, I am starting with bats and I love it. Now I have encountered a problem to test part of the functionality of my script.

My script parses some data, calls an api and depending on the response it continues or not the execution.

Would it be possible to predefine the value of a variable used in the script and not modify it?
Something like declare -r response="My fake response", a constant that works throughout the execution of the script within the test.
And then in my script, where I read the variable response I will always read "My fake response" and it does not change even if another value is assigned in the script to test.

I do not know if I have explained well. I hope someone has some idea how to do this.

@martin-schulze-vireso
Copy link
Member

martin-schulze-vireso commented Jun 23, 2023

If I understand you correctly, you want to simulate responses, which sounds a lot like mocking. maybe you want to look into mocking libraries for Bats.

Anyway, your intended solution might have several issues:

  1. Bash readonly variables trigger errors on writing, which is not what you want
  2. Subshells will only inherit exported variables
  3. Mocking every variable named response might have unintended consequences due to name collisions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants