【问题标题】:Why is recaptcha not causing this test to fail?为什么 recaptcha 不会导致此测试失败?
【发布时间】:2012-12-03 22:13:18
【问题描述】:

我正在使用 Rails 3.1.0 和来自here 的recaptcha gem。我正在运行一个黄瓜测试,检查用户是否可以注册。注册需要用户填写验证码。我知道我的测试没有触及验证码:

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

但测试仍然通过。我通过验证页面上是否存在成功注册消息并且使用此步骤不存在重新验证失败消息来检查成功:

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

控制器与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

recaptcha 在测试环境中不起作用有什么原因吗?它似乎在开发中运行良好。

【问题讨论】:

    标签: ruby-on-rails cucumber recaptcha


    【解决方案1】:

    默认情况下,Recaptcha 不会在 test 和 cucumber 环境中验证验证码(请参阅 verify logicconfiguration logicdefault value)。如果不是这样,要么测试会非常困难,要么验证码不会很有用。

    【讨论】:

    • 我认为可能是这种情况,但在 github 页面上的任何地方都没有看到它。没看源码。
    【解决方案2】:

    只是添加到 Bens 的答案并给出一个潜在的解决方法:

    为了确保我已经完成的 RSpec 中的事情不起作用(它“因 recaptcha 失败”)

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

    谢谢本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      相关资源
      最近更新 更多