mushroom
mushroom

Reputation: 6289

Why is recaptcha not causing this test to fail?

I am using Rails 3.1.0 and the recaptcha gem from here. I was running a cucumber test that checks that users can sign up. Sign up requires users to fill out the captcha. I know that my test does not touch the captcha:

When /^I create a new account with email: "(.*?)" and password: "(.*?)"$/ do |email, pw|
    click_link "Sign up"
    fill_in "Email", :with => email
    fill_in "Password", :with => pw
    fill_in "Password confirmation", :with => pw
    click_button "Sign up"
end

But the test still passes. I check success by verifying that the successful sign up message is present on the page and the recaptcha failure message is not present using this step:

Then /^I should (not )?see "(.*)"$/ do |negate, text|
  if negate
    page.should_not have_content text
  else
    page.should have_content text
  end
end

The controller is almost identical to the first one suggested here.

  class RegistrationsController < Devise::RegistrationsController
    def create
      if verify_recaptcha
        super
      else
        build_resource
        clean_up_passwords(resource)
        flash.now[:alert] = "There was an error with the recaptcha code below. Please re-enter the code."      
        flash.delete :recaptcha_error
        render :new
      end
    end
  end

Is there any reason why recaptcha would not work in the test environment? It seems to work fine in development.

Upvotes: 3

Views: 1654

Answers (2)

iNulty
iNulty

Reputation: 923

Just to add to Bens answer and give a potential workaround:

To make sure things don't work (it "fails with recaptcha") in RSpec I've done

before do
  Recaptcha.configuration.skip_verify_env.delete('test')
end

after do
  Recaptcha.configuration.skip_verify_env << 'test'
end

Thanks Ben

Upvotes: 4

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

Recaptcha by default does not verify the captcha in the test and cucumber environments (see verify logic, configuration logic, and default value ). If it weren't for this, either testing would be very difficult or the captcha wouldn't be very useful.

Upvotes: 5

Related Questions