StevenMcD
StevenMcD

Reputation: 17482

Data inserted in Cucumber tests not populating Dropdown

DISCLAIMER: I am still a Rails/Cucumber newbie but I am trying to learn. I am still using the "web_steps" for Cucumber while I work through the RailsInAction book and I have experimented with writing my own steps. I understand why they were taken out and I will get there.

In one of my Cucumber Scenarios I am populating some data and trying to use that data to populate a dropdown. The data is definitely being inserted, I know this because I wrote a step to test to see if the data is in fact there and it is.

When then DropDown is suppose to populated and I try "select" from that DropDown I get the error "*cannot select option, no option with text 'Steven' in select box 'result_winner' (Capybara::ElementNotFound)*"

I am sure I am missing something basic but I have lost about an hour to this now and I just need some help.

The tests:

When I follow "Record new Result"
And the users "Steven, Joshua" exist
And I select "Steven" from "result_winner"

The test steps:

When /^the users "([^"]*)" exist$/ do |playerNames|
  @names = playerNames.scan(/[\w']+/)

  for name in @names
    @newPlayer = Player.new
    @newPlayer.name = name
    @newPlayer.save
  end
end

Then the view code in the _form.html.erb:

<p>
    <%= f.label :winner %>
    <%= select("result", "winner", @players.map {|p| [p.name, p.id]}) %>
</p>

The Controller:

  def new
    @result = Result.new
    @players = Player.find(:all)
  end

If I manually populate the development database with names and load it up in the browser, the dropdowns have data populated in them and work perfectly. I am pretty sure I am misunderstanding when/where test data is visible, or how to test for and select data from a dropdown.

Any help would be greatly appreciated.

Upvotes: 2

Views: 1170

Answers (2)

Exrelev
Exrelev

Reputation: 25

Your "the users "Steven, Joshua" exist" should be in a Given step.

It describes a state before an action is done.

Given the users "Steven, Joshua" exist
When I follow "Record new Result"
And I select "Steven" from "result_winner"

Upvotes: 0

Jon M
Jon M

Reputation: 11705

It looks to me like a simple problem with the step sequence. You're visiting the page, and then populating the database, but the page doesn't get reloaded to take account of the new database values. If you swap the order of the first 2 steps around, I expect that would fix it.

To be clear, cucumber will literally step through your scenario one line at a time:

  1. Load up the webpage (which doesn't contain your test data)
  2. Insert your data into the DB
  3. Look for that data in the dropdown (which won't be there as the page was loaded before the data was in the DB)

Upvotes: 3

Related Questions