使用 Recaptcha 进行 Rails 功能测试

发布于 2024-08-22 17:24:19 字数 1660 浏览 6 评论 0原文

我的 UsersControllerTest 目前失败,因为我在 UsersController#create 中使用 verify_recaptcha。如何编写测试,以便使用 params[:user] 传递已知良好的验证码响应?我正在使用 reCAPTCHA,但我想这个问题适用于任何验证码实现。

这是我的 UsersController#create

def create
  @user = User.new(params[:user])    
  if verify_recaptcha(@user) && @user.save
    flash[:notice] = "Account registered!"
    redirect_to new_order_url
  else
    flash.now[:error] = "Account not registered!"
    render :action => :new
  end
end

这是我的功能测试

test "should create user" do
    assert_difference('User.count') do
      post :create, :user => { :login => "jdoe",
                               :password => "secret", 
                               :password_confirmation => "secret",
                               :first_name => 'john',
                               :last_name => 'doe',
                               :address1 => '123 Main St.',
                               :city => 'Anytown',
                               :state => 'XY',
                               :zip => '99999',
                               :country => 'United States',
                               :email => '[email protected]' }
    end
end

此测试失败如下

  4) Failure:
test_should_create_user(UsersControllerTest)
    [(eval):3:in `each_with_index'
     /test/functional/users_controller_test.rb:15:in `test_should_create_user']:
"User.count" didn't change by 1.
<3> expected but was
<2>.

My UsersControllerTest currently fails because I use verify_recaptcha in UsersController#create. How can I write my tests such that a known good CAPTCHA response is passed with params[:user]? I am using reCAPTCHA, but I suppose the question would apply to any CAPTCHA implementation.

Here is my UsersController#create

def create
  @user = User.new(params[:user])    
  if verify_recaptcha(@user) && @user.save
    flash[:notice] = "Account registered!"
    redirect_to new_order_url
  else
    flash.now[:error] = "Account not registered!"
    render :action => :new
  end
end

and here is my functional test

test "should create user" do
    assert_difference('User.count') do
      post :create, :user => { :login => "jdoe",
                               :password => "secret", 
                               :password_confirmation => "secret",
                               :first_name => 'john',
                               :last_name => 'doe',
                               :address1 => '123 Main St.',
                               :city => 'Anytown',
                               :state => 'XY',
                               :zip => '99999',
                               :country => 'United States',
                               :email => '[email protected]' }
    end
end

This test fails as follows

  4) Failure:
test_should_create_user(UsersControllerTest)
    [(eval):3:in `each_with_index'
     /test/functional/users_controller_test.rb:15:in `test_should_create_user']:
"User.count" didn't change by 1.
<3> expected but was
<2>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-08-29 17:24:19

尝试使用flexmock或mocha让verify_recaptcha的所有实例返回true:

我的应用程序中的这一行使创建测试通过,在我的应用程序上没有问题:

flexmock(User).new_instances.should_receive(:verify_recaptcha).and_return(true)

如果您在“创建”操作发生之前添加这一行,它应该可以工作。

另外,我还没有尝试过这个 recaptcha 插件,但这也可能对你有帮助:
http://www.fromdelhi。 com/2006/07/21/rails-captcha-and-testing-using-mock-objects/

Try using flexmock or mocha to have all instances of verify_recaptcha return true:

This line in my app made the create test pass without a problem on my app:

flexmock(User).new_instances.should_receive(:verify_recaptcha).and_return(true)

If you add this line before the "create" action takes place it should work.

Also, I haven't tried out this recaptcha plugin, but this might also be of help for you:
http://www.fromdelhi.com/2006/07/21/rails-captcha-and-testing-using-mock-objects/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文