0
votes

I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2 with the Selenium driver. I have an issue about concurrency on running JavaScript scenarios (supposed that my case is related to a concurrency issue).

In my .feature file I have:

Feature: ...
  ...

  @javascript
  Scenario: ...
    Given ...
    When I log out
    Then I should be redirected to the home page

In my step definition .rb file I have:

Then /^I should be redirected to the home page$/ do
  current_path.should == root_path
end

When I run the cucumber command line in the Terminal window so to run tests, I get this error:

$ cucumber
Using the default profile...

Feature: ...
  ...

  @javascript
  Scenario: ...
    ...
    When I log out
    Then I should be redirected to the home page
      expected: "/"
        got: "/user" (using ==) (RSpec::Expectations::ExpectationNotMetError)
        ...

However if in my step definition .rb file I use

Then /^I should be redirected to the home page$/ do
  sleep(1)

  current_path.should == root_path
end

it works as expected: all tests pass.

So, even if the problem seems to be solved, there is a better way to handle my issue? if so, what is the "right"/"common used" solution (maybe using some Capybara API)?

1

1 Answers

1
votes

It isn't concurrency. The problem is that cucumber isn't synchronized with your application. The test side doesn't know anything about whether your action has completed its execution or not. So when you're asserting the path your application hasn't generated the response to actually do redirect.

If you'll take a look into capybara you'll see that it polls for the element to appear. (With guard max wait time). So you can either take the same approach and do a polling for some time or you can take a lazier approach and in your test before testing for path add a step that will check for some content on the target page to appear. So you can rely on capybara's built in mechanism.