1
我已经为Devise注册编写了一些自定义代码。也就是说,一旦用户点击“注册”按钮,Devise的通用代码将确保用户保存正确。但是,我希望进入并确保用户获得关联的Stripe客户帐户。大多数下面的代码是从设计,除了我添加了几行:RSpec控制器测试错误:控制器没有实现方法
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
,我自定义的方法:
- 获取正确的参数(即,还有其他测试,以确保该方法做正确的事情,与那些参数),并返回true,借此然后将用户重定向到
after_sign_up_path
。在这个例子中,正确的参数是,sign_up_params[:token]
正在通过以create_stripe_customer
- 获取错误的参数传递和返回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
不知道为什么控制器没有实现该方法...
好的我修复了这个问题,但后来又遇到了另一个问题。这可能是无关的,并且是一个Devise的东西,但通过阅读文档,它似乎我添加的映射代码不工作...我试图添加他们的存根(stub)按照说明在这里:https://github.com/plataformatec/设计/维基/操作方法:-Stub认证功能于控制器的规格。但不管我做什么,我总是得到'NoMethodError: 未定义的方法'认证? for nil:NilClass',它位于'devise_controller.rb:105'中的'warden'对象上。有什么想法吗? – james
最好为此发布一个新问题。 –
是我做的,如果您有任何意见请随时分享! http://stackoverflow.com/questions/30945840/test-custom-devise-registration-controller-no-method-error-authenticated-on – james