【问题标题】:RSpec controller test error: controller does not implement methodRSpec控制器测试错误:控制器没有实现方法
【发布时间】:2015-06-18 00:21:38
【问题描述】:

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

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 控制器上编写测试,以确保在 postcreate 上,我的自定义方法:

  1. 获取正确的参数(即,还有其他测试来确保该方法对这些参数执行正确的操作),然后返回 true,然后将用户重定向到 after_sign_up_path。在此示例中,正确的参数是 sign_up_params[:token] 正在传递给 create_stripe_customer
  2. 获取错误的参数并返回 false,从而根据我的自定义代码将用户重定向到 root_path

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

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

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

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

不知道为什么控制器没有实现该方法...

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 好的,我解决了这个问题,但后来又遇到了另一个问题。这可能是不相关的,并且是设计的东西,但是在阅读了文档后,我添加的映射代码似乎不起作用......我尝试按照此处的说明添加他们的存根:github.com/plataformatec/devise/wiki/…。但无论我做什么,我都会不断收到NoMethodError: undefined method authenticated?对于 nil:NilClass`,它位于 devise_controller.rb:105 中的 warden 对象上。有什么想法吗?
    • 最好为此发布一个新问题。
    • 是的,我做到了,如果您有任何 cmet,请随时分享! stackoverflow.com/questions/30945840/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    相关资源
    最近更新 更多