2

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

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

  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

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...

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

OK I fixed that, but then I got another problem. It's likely unrelated, and a Devise thing, but having read through the docs, it appears the mapping code I added isn't working... I tried to add their stub following instructions here: github.com/plataformatec/devise/wiki/…. But regardless of what I do, I keep getting NoMethodError: undefined method authenticated?' for nil:NilClass` which is on the warden object in the devise_controller.rb:105. Any thoughts?
It would be better to post a new question for this.
Yup I did, if you have any comments please do feel free to share! stackoverflow.com/questions/30945840/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.