Integration tests that adapt to changing requirements

In the test-driven videos, when we are interacting with the page through Capybara, we are saying:

click_link 'Add a new todo'

One benefit of this is that if a requirement is that ‘Add a new todo’ should be the content of the link, we are validating that. But let’s say down the line, the requirements change, and adding a new todo is done by clicking a button with a plus sign, or even more simply, the content of the link changes. Our tests would break for something that seems rather minor. It seems like our tests are too coupled to the content of the view, rather than the behavior of the application. Is this a best practice, or should we rather target the ID of the link?

Obviously this is a trivial example, but I guess on a more macro level, should our integration tests follow the exact behavior of a user (explicitly say what the user will see, if that’s possible) or more the behavior of the application?

Thanks!

Hi @dolphorama,

You could use a rel attribute on your link that helps to describe the behaviour of the action you want the user to do, and then query that in capybara. For example:

<a href="" rel="add-todo">Add a new todo</a>

Sorry for the ugliness, for some reason the forum keeps stripping out the attributes… @learnbot what’s up with that?

Then use:

find('[rel="add-todo"]').click

This would allow the content of the link to change, and even the HTML element too, without breaking your test.

See this for more: Write better cukes with the rel attribute | Next.js Blog Example with Markdown

Hope that helps!

Dan

1 Like

To mitigate text changes, consider using i18n: Better Tests Through Internationalization