簡體   English   中英

使用Recaptcha進行Rails功能測試

[英]Rails Functional Test using Recaptcha

我的UsersControllerTest當前失敗,因為我在UsersController #create中使用verify_recaptcha。 如何編寫我的測試,以便使用params [:user]傳遞已知良好的CAPTCHA響應? 我正在使用reCAPTCHA,但我想這個問題適用於任何CAPTCHA實現。

這是我的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 => 'jdoe@example.com' }
    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>.

嘗試使用flexmock或mocha使verify_recaptcha的所有實例都返回true:

我的應用程序中的這一行使我的應用程序創建測試通過沒有問題:

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

如果在“創建”操作發生之前添加此行,它應該可以工作。

此外,我還沒有試過這個rec​​aptcha插件,但這也可能對你有所幫助: http//www.fromdelhi.com/2006/07/21/rails-captcha-and-testing-using-mock-對象/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM