Rails Functional Test Using Recaptcha

My UsersControllerTest is currently failing because I am using verify_recaptcha in UserController # create. How can I write my tests in such a way that the well-known good CAPTCHA answer is passed with the [: user] parameters? I am using reCAPTCHA, but I believe this question applies to any implementation of CAPTCHA.

Here is my UserController # 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 => ' jdoe@example.com ' } 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>. 
+4
source share
1 answer

Try using flexmock or mocha so that all verify_recaptcha instances return true:

This line in my application made a test creation pass without any problems in my application:

 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 have not tried this recaptcha plugin, but it can also help you: http://www.fromdelhi.com/2006/07/21/rails-captcha-and-testing-using-mock-objects/

+5
source

Source: https://habr.com/ru/post/1302505/


All Articles