Last active
December 12, 2015 01:28
-
-
Save shingara/4691019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserCreate | |
def initialize(params) | |
@params = params | |
end | |
attr_reader :params | |
def user | |
@user ||= User.new(params[:user]) | |
end | |
def create | |
add_address | |
user.save | |
end | |
private | |
def add_address | |
@user.build_address(params[:address]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirement in the user_create.rb. Not inside to this example | |
require 'user' | |
require 'address' | |
## | |
#require 'user_create' | |
require 'spec_helper' # All rails stack | |
describe UserCreate, "integrations" do | |
let(:user_create) { UserCreate.new({:user => {'name' => 'Cyril'}, :address => {'num' => 46}}) } | |
describe "#user" do | |
it 'initialize a user with params pass in args' do | |
expect(user_create.user).to be_a(User) | |
expect(user_create.user.name).to eq 'Cyril' | |
end | |
it 'memoize the user initialize' do | |
expect(user_create.user).to eq user_create.user | |
end | |
end | |
describe "#save" do | |
it 'save the user' do | |
expect { | |
user_create.save | |
}.to change(User.count).by(1) | |
end | |
it 'add address' do | |
expect { | |
user_create.save | |
}.to change(Address.count).by(1) | |
expect(user_create.user.address).to be_a(Address) | |
expect(user_create.user.address.num).to eq 46 | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirement in the user_create.rb. Not inside to this example | |
require 'user' | |
require 'address' | |
## | |
require 'user_create' | |
describe UserCreate, "without DB connection" do | |
let(:user_create) { UserCreate.new({:user => {'name' => 'Cyril'}, :address => {'num' => 46}}) } | |
describe "#user" do | |
it 'initialize a user with params pass in args' do | |
expect(user_create.user).to be_a(User) | |
expect(user_create.user.name).to eq 'Cyril' | |
end | |
it 'memoize the user initialize' do | |
expect(user_create.user).to eq user_create.user | |
end | |
end | |
describe "#save" do | |
before do | |
user_create.user.should_receive(:save).and_return(true) | |
end | |
it 'save the user' do | |
user_create.save | |
end | |
it 'add address' do | |
user_create.save | |
expect(user_create.user.address).to be_a(Address) | |
expect(user_create.user.address.num).to eq 46 | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'user_create' | |
describe UserCreate, "without User model requirement" do | |
before do | |
User.stub(:new).with({'name' => 'Cyril'}).and_return(user) | |
end | |
let(:user_create) { UserCreate.new({:user => {'name' => 'Cyril'}, :address => {'num' => 46}}) } | |
let(:user) {mock(:user, :save => true) } | |
describe "#user" do | |
it 'initialize a user with params pass in args' do | |
expect(user_create.user).to be_a(User) | |
end | |
it 'memoize the user initialize' do | |
expect(user_create.user).to eq user_create.user | |
end | |
end | |
describe "#save" do | |
it 'save the user' do | |
user.should_receive(:save).and_return(true) | |
user_create.save | |
end | |
it 'add address' do | |
user.should_receive(:build_address).with({'num' => 46}) | |
user_create.save | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment