RSpec控制器测试错误:控制器未实现方法 [英] RSpec controller test error: controller does not implement method

查看:118
本文介绍了RSpec控制器测试错误:控制器未实现方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Devise注册编写了一些自定义代码。也就是说,一旦用户点击注册按钮,Devise的通用代码就可以确保正确保存用户。但是,然后,我要确保用户获得关联的Stripe客户帐户。下面的大多数代码来自Devise,除了我添加的几行:

I've written some custom code for Devise registration. Namely once a user hits the "sign up" button, Devise's generic code ensures that the user is saved correctly. But then, I want to go in and make sure that the user gets an associated Stripe customer account. Most of the code below is from Devise, except for a few lines that I added:

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

我现在尝试在 create 控制器上编写测试,以确保在发布到创建,这是我的自定义方法:

I'm trying now to write tests on the create controller to make sure that upon a post to create, my custom method:


  1. 获取正确的参数(即,进行其他测试以确保该方法对这些参数执行正确的操作),并返回true,从而将用户重定向到 after_sign_up_path 。在此示例中,正确的参数是将 sign_up_params [:token] 传递给 create_stripe_customer

  2. 获取错误的参数并返回false,从而根据我的自定义代码将用户重定向到 root_path

  1. Gets the right parameters (i.e., there are other tests to make sure that the method does the right things with those parameters), and returns true, whereby then the user is redirected to the after_sign_up_path. In this example, the right parameter is that the sign_up_params[:token] is being passed through to create_stripe_customer
  2. Gets the wrong parameters and returns false, whereby the user is redirected to the root_path as per my custom code

到目前为止,这是我的RSpec文件:

This is my RSpec file so far:

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

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

When I run it however, I get:

Failure/Error: Users::RegistrationsController.should_receive(:create_stripe_customer).with(source: "test" , user_id: @user.id).and_return(false)
   Users::RegistrationsController does not implement: create_stripe_customer

不确定为什么控制器未实现该方法...

Not sure why the controller is not implementing the method...

推荐答案

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

You're stubbing create_stripe_customer as a class method, but it's actually an instance method.

这篇关于RSpec控制器测试错误:控制器未实现方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆