设计Rspec测试不通过。无法弄清楚为什么

时间:2012-12-01 03:08:08

标签: ruby-on-rails rspec devise

我有一个应用程序,其中我正在扩展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,看看是否会造成一些麻烦,但它没有效果。

思想?

1 个答案:

答案 0 :(得分:0)

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

这会在RegistrationsController 上存根消息verify_recaptcha。这不起作用,因为create在RegistrationsController的实例上调用。

为了这个目的,rspec-rails有一个恰当命名的辅助方法controller

controller.stub(:verify_recaptcha) { true }
相关问题