0

我有一个应用程序,我正在扩展 Devise 的 RegistrationsController 以添加一些我自己的功能,但由于某种原因,我无法通过我的帐户创建测试。在开发中一切正常,所以我几乎可以肯定这与我编写测试的方式有关,但老实说,我现在愿意尝试任何事情。太令人沮丧了。

这是我的测试的摘录。您会看到它正在检查新用户,但没有找到。

describe RegistrationsController do
  before :each do
    @request.env['devise.mapping'] = Devise.mappings[:user]
    @new_user = { 'fname' => 'Stizzle',
                  'lname' => 'Mcgizzle',
                  'email' => 'qwerty@example.com',
                  'password' => 'foobar'
                }
  end
  describe "During a regular registration, " do
    it "filling out the fields correctly should create a new user" do
      RegistrationsController.stub(:verify_recaptcha).and_return(true)
        expect do
          post  :create,
          :user => @new_user,
          :recaptcha_response_field => 'foobar',
          'tos-agree' => 'true'
          end.to change(User.all, :count).by(1)
        end

我的控制器动作:

def create
  build_resource
  if verify_recaptcha(:model => resource, :message => "The text you entered did not match.  Please try again.") && resource.save
    if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_navigational_format?
      sign_in(resource_name, resource)
      respond_with resource, :location => after_sign_up_path_for(resource)
    else
      set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
      expire_session_data_after_sign_in!
      respond_with resource, :location => after_inactive_sign_up_path_for(resource)
    end
  else
    clean_up_passwords resource
    render :action => 'new'
  end
end

我尝试从控制器中删除 CAPTCHA 只是为了看看这是否会造成一些麻烦,但它没有任何效果。

想法?

4

1 回答 1

0
RegistrationsController.stub(:verify_recaptcha).and_return(true)

verify_recaptcha这会在 RegistrationsController上存根消息。那是行不通的,因为在RegistrationsController的实例create上调用。

rspec-railscontroller为此目的提供了恰当命名的辅助方法:

controller.stub(:verify_recaptcha) { true }
于 2012-12-04T04:15:17.767 回答