Search code examples
rubycucumbercaptchabddruby-on-rails-3

Cucumber feature on form with Captcha


If you form has captcha (i'm using humanizer gem). How do you fill up form and send and get the expected result when writing cucumber feature.

Scenario: Sign Up with Valid Data
  Given I am not authenticated
  And I am on the sign in page
  When I follow "Sign up"
  And I fill in the following:
    | Name                   | Administrator            |    
    | Email                  | admin@admin.com          |
    | Password               | 123456                   |
    | Password confirmation  | 123456                   |
  And I fill in the captcha correctly
  And I press "Sign Up"
  Then I should be on the new_company page
  And I should see "Hello Manoj"

Now i can write a step definition matching /^I fill up catcha correctly$/ but what must be put there?

Be gentle, i'm new to cucumber and it's been frustrating so far. I'm not new to Rails or programming otherwise.


Solution

  • You're right, Aditya. Putting environment dependent code in models is not good solution. But you can "stub" bypass_humanizer? when needed:

    # user.rb
    class User
      include Humanizer
    
      require_human_on :create, :unless => :bypass_humanizer?
    
      protected
    
      def bypass_humanizer?
        false
      end
    end
    
    # step definitions for your scenarion
    And /^I fill in the captcha correctly$/ do
      # from now any instance of User class will skip require_human_on validator
      User.any_instance.stubs(:bypass_humanizer?).returns(true)
    end
    
    # in Gemfile
    group :development, :test do
      gem "mocha"
    end
    

    Now you have a model with environment agnostic code and you can put it in the specific state any time you need (for testing, of course).