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>.
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/