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