我的 UsersControllerTest 当前失败,因为我在 UsersController#create 中使用了 verify_recaptcha。如何编写测试,以便使用 params[:user] 传递已知良好的 CAPTCHA 响应?我正在使用 reCAPTCHA,但我想这个问题将适用于任何 CAPTCHA 实现。
这是我的用户控制器#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>.