2010-12-20 55 views
2

如果您的表单有验证码(我正在使用人性化的宝石)。如何填写表格并在写黄瓜功能时发送并获得预期结果。验证码形式的黄瓜功能

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     | [email protected]   | 
    | 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" 

现在我可以写一个步骤定义匹配/ ^我正确填写catcha $ /但是必须放在那里?

温柔一点,我是黄瓜新手,到目前为止一直令人沮丧。我不是新手Rails或编程,否则。

回答

2

你是对的,Aditya。将与环境相关的代码放在模型中并不是很好的解决方案但你可以在需要的“存根” bypass_humanizer?时:

# 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 

现在你有一个环境无关的代码模型,你可以把它放在特定的状态,你需要的任何时间(用于测试,当然)。

2

因此,我发现的一个解决方案是确保Captcha仅在生产环境中添加。

在某种程度上,我对此感到满意。但是,这将是减少应用程序中基于环境的分支的理想选择。

class User 
    ... 
    include Humanizer 
    if Rails.env.production? 
    require_human_on :create, :unless => :bypass_humanizer 
    end 
    ... 
end 
+0

是的,这是最合理的解决方案。试想一下步骤的定义“并且我正确填写captcha”:-) – 2010-12-21 11:20:47

+0

您是否仅在测试中禁用它?这种验证码可以在本地和分期中使用。 – 2016-04-16 08:54:06