https://cypress.io logo
Are there any best practices established for how t...
# best-practices
p
Are there any best practices established for how to test random application behavior? We have a math problem website and some of our student experiences involve students getting a random problem. For example, the students might get a random problem from a pool of 100 problems and work until the student gets 3 right in a row. What's an appropriate way to test this? My thought is to try to cross-reference DOM data with data in the application store (pinia) to identify which math problem we're displaying in order to answer it correctly/incorrectly based on the testing requirements
This feels a lot like conditional testing, which I know is a no-no, but I don't see a clear alternative
r
do you have access to the problems? are they stored in a database? does the front-end get them via an api?
p
Thanks for your reply! 1. The problems (and the problem sets which determine which problems you get and in what order) are sent via API requests 2. We're mocking the API using miragejs, so the problems do technically live on the frontend, but I think it'd be prefereable for the test code to treat them as if they were only available via API requests (though I'm open to sensible alternatives)
We're also exposing the pinia store to cypress via the window object, so one thing we could do would be to use the pinia store to determine which problem is currently displayed and to get it's correct answer that way
That sounds like the most promising option to me, but I'd love to hear other folks' thoughts on the matter of testing areas of application behavior where randomness is involved
f
> We're mocking the API using miragejs, so the problems do technically live on the frontend, If the content of the questions is irrelevant, what about stubbing the responses with cy.intercept instead? Then you could present the same questions each time, and control what is/isn't a correct answer
p
Interesting prospect, thank you. I suppose it doesn’t let you test that a random choice is made by the system, but I don’t see a way to assert that anyway