RSpec控制器测试错误:控制器没有实现方法

时间:2015-06-18 00:21:39

标签: ruby-on-rails ruby-on-rails-4 rspec devise

我为Devise注册编写了一些自定义代码。即,一旦用户点击"注册"按钮,设计的通用代码可确保正确保存用户。但是,我想进入并确保用户获得相关的Stripe客户帐户。下面的大部分代码来自Devise,除了我添加的几行:

var reg_letters = /^[A-ZÆØÅa-zæøå]+$/;

我现在正尝试在class Users::RegistrationsController < Devise::RegistrationsController def create build_resource(sign_up_params) resource.save yield resource if block_given? if resource.persisted? #### If statement below is the key custom code #### if create_stripe_customer #### Back to default Devise code #### if resource.active_for_authentication? set_flash_message :success, :signed_up if is_flashing_format? sign_up(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_flashing_format? expire_data_after_sign_in! respond_with resource, location: after_inactive_sign_up_path_for(resource) end #### Else statement below is custom #### else flash[:danger] = flash_messages["not_your_fault"] redirect_to root_path end else clean_up_passwords resource set_minimum_password_length respond_with resource end end private def create_stripe_customer new_stripe_customer = StripeService.new({ source: sign_up_params[:token], user_id: self.id }) if new_stripe_customer && self.update_attributes(stripe_customer_id: new_stripe_customer.id) else false end end end 控制器上编写测试,以确保在createpost时,我的自定义方法:

  1. 获取正确的参数(即,还有其他测试以确保方法使用这些参数执行正确的操作),并返回true,然后将用户重定向到create。在此示例中,正确的参数是after_sign_up_path正在传递到sign_up_params[:token]
  2. 获取错误的参数并返回false,根据我的自定义代码将用户重定向到create_stripe_customer
  3. 到目前为止,这是我的RSpec文件:

    root_path

    但是,当我运行它时,我得到:

    describe Users::RegistrationsController, "Create action" do
    
      before do
        request.env['devise.mapping'] = Devise.mappings[:user]
      end
    
      it "Successful creation" do
        Users::RegistrationsController.should_receive(:create_stripe_customer).with(source: "test" , user_id: @user.id).and_return(false)
        post :create, sign_up: { first_stripe_token: "test" }
      end
    end
    

    不确定为什么控制器没有实现方法......

1 个答案:

答案 0 :(得分:1)

您将create_stripe_customer作为类方法存根,但它实际上是一个实例方法。