如果你形成了验证码(我正在使用人性化宝石)。在编写黄瓜特征时,如何填写表格并发送并获得预期结果。
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"
现在我可以写一个步骤定义匹配/ ^我正确填写catcha $ /但必须放在哪里?
温柔,我是黄瓜新手,到目前为止一直令人沮丧。我不是Rails的新手或其他编程。答案 0 :(得分:2)
因此,我发现一个解决方案是确保仅在生产环境中添加Captcha。
在某种程度上,我对此很满意。但是,在应用程序中减少基于环境的分支是理想的。
class User
...
include Humanizer
if Rails.env.production?
require_human_on :create, :unless => :bypass_humanizer
end
...
end
答案 1 :(得分: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
现在您拥有一个具有环境无关代码的模型,您可以随时将其置于特定状态(当然,用于测试)。