← Back to Upcase

Integration tests that adapt to changing requirements

(Dolph Mullen) #1

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?


(Dan Scotton) #2

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:


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

See this for more: http://blog.steveklabnik.com/posts/2011-12-20-write-better-cukes-with-the-rel-attribute

Hope that helps!


(Derek Prior) #3

To mitigate text changes, consider using i18n: http://robots.thoughtbot.com/better-tests-through-internationalization/