RSpec controller test error: controller does not implement method
RSpec controller test error: controller does not implement method
我已经为 Devise 注册编写了一些自定义代码。也就是说,一旦用户点击 "sign up" 按钮,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
控制器上编写测试,以确保在 post
到 create
时,我的自定义方法:
- 获取正确的参数(即,有其他测试可确保该方法使用这些参数做正确的事情),并且 returns 为真,然后用户将被重定向到
after_sign_up_path
.在这个例子中,正确的参数是 sign_up_params[:token]
被传递给 create_stripe_customer
- 获取错误的参数和 returns 错误,根据我的自定义代码
,用户被重定向到 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
不确定为什么控制器没有执行该方法...
您将 create_stripe_customer
存根为 class 方法,但它实际上是一个实例方法。
我已经为 Devise 注册编写了一些自定义代码。也就是说,一旦用户点击 "sign up" 按钮,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
控制器上编写测试,以确保在 post
到 create
时,我的自定义方法:
- 获取正确的参数(即,有其他测试可确保该方法使用这些参数做正确的事情),并且 returns 为真,然后用户将被重定向到
after_sign_up_path
.在这个例子中,正确的参数是sign_up_params[:token]
被传递给create_stripe_customer
- 获取错误的参数和 returns 错误,根据我的自定义代码 ,用户被重定向到
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
不确定为什么控制器没有执行该方法...
您将 create_stripe_customer
存根为 class 方法,但它实际上是一个实例方法。