Jun 22 stubbing captchas in cucumber

tags: captcha stub cucumber | comments

You use some captcha solution to avoid bots in your site, and you are a good kid and like everything well test-covered. Well, don’t sweat too much with weird solutions to validate your captcha, it’s an external thing you know, it’s pretty ok to stub it.

Here is what I did for simplicity:

# in features/support/recaptcha_helper.rb
module RecaptchaHelper
  def captcha_evaluates_to(result)
    eval <<-CODE
      module ::Recaptcha
        module Verify
          def verify_recaptcha
            #{result}
          end
        end
      end
CODE
  end
end

Then include it in the World of course. And use it like this:

# in features/step_definitions/recaptcha_steps.rb
When /^I fill the captcha incorrectly$/ do
  captcha_evaluates_to false
end

When /^I fill the captcha correctly$/ do
  captcha_evaluates_to true
end

The technique used, opening a class and changing it in your tests, should be used with care. Read more in this thread in the cukes list.

blog comments powered by Disqus