I am trying to do some Cucumber testing like the following:
Given I am logged in as admin
When I go to the article page
Then I should see "Edit Article"
And I should see "Article Title"
where the path to the article page is “articles/1” or some known id.The problem is that when I insert my data using my step definition
Article.create(:name => "Article Title")
I can’t know ahead of time what the id will be when the record is inserted.
Essentially, I either need
a) to be able to insert predictable ids. I’ve tried the method mentioned in the answer of this question but it doesn’t seem to work for me – it always inserts a different id.
or
b) write my cucumber steps in such a way that I can pass in a parameter for my Article id
All Cucumber tutorials I’ve seen sort of gloss over this type of scenario – it’s always going to the list of all Articles or whatever, never a specific detail page.
Any help would be appreciated. Thanks!
There are two ways to create predictable ID values. The first is to manually set the ID on create:
The catch here is that options passed to either new, create, or update_attributes will not affect the id value which is why the call to model.id is, unfortunately, required.
An alternative is to reset the whole table before running your tests. This can be altered to account for fixtures if required. For example:
This will set the ID of the first row inserted to be a predictable 1.