Skip to content

Instantly share code, notes, and snippets.

@lolaodelola
Last active July 18, 2021 14:14
Show Gist options
  • Save lolaodelola/e2976736583230972c5658b879fe6d4e to your computer and use it in GitHub Desktop.
Save lolaodelola/e2976736583230972c5658b879fe6d4e to your computer and use it in GitHub Desktop.
pw.rb
class Customer < ApplicationRecord
validates_presence_of :first_name, :last_name, :phone
validates_uniqueness_of :phone
# Implicit validations on presence being done
has_secure_password
has_secure_password :password_confirmation
has_secure_password :recovery_password, validations: false
end
======
class CustomersController < ApplicationController
...
def create
@customer = Customer.new(customer_params)
if @customer.save!
redirect_to root_path
else
render "new"
end
end
private
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone, :password, :password_confirmation)
end
end
=====
RSpec.describe "Customers", type: :request do
describe "POST customers#create" do
it 'create user with valid attributes' do
customer_params = attributes_for(:verified_customer)
post customers_url, :params => customer_params.to_json, :headers => { "Content-Type": "application/json" }
expect(response).to have_http_status(200)
end
end
end
=====
# Factory
password = Faker::Alphanumeric.alphanumeric(number: 10)
factory :verified_customer, class: Customer do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
phone { Faker::PhoneNumber.phone_number }
password { password }
password_confirmation { password }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment