Created
September 6, 2017 16:26
-
-
Save staycreativedesign/c2c13c909956374fb67b31a61e81d74b to your computer and use it in GitHub Desktop.
test not passing?
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 CreatesPoint | |
def initialize(user_id,point) | |
@user_id = user_id | |
@point = point | |
end | |
def run! | |
create_point | |
end | |
private | |
def create_point | |
binding.pry | |
user = User.find_by(referral_code: @user_id) | |
binding.pry | |
user.points += @point | |
binding.pry | |
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
class CreatesUser | |
def initialize(params) | |
@user_params = params | |
end | |
def run! | |
create_user | |
end | |
private | |
def create_user | |
@user = User.new(user_params) | |
if @user.save | |
create_user_account | |
CreatesPoint.new(@user.referrer, 1).run! | |
true | |
else | |
false | |
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
Failures: | |
1) UsersController#create adds a point to the referrer | |
Failure/Error: expect(@ref.points).to eq 1 | |
expected: 1 | |
got: 0 | |
(compared using ==) |
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
https://www.screencast.com/t/mfjf6ImhfYnP | |
# view to see whats happening | |
require 'rails_helper' | |
RSpec.describe UsersController do | |
before(:each) do | |
@ref = create(:user) | |
end | |
describe '#create' do | |
it 'sets a referral code' do | |
post :create, params: create_user_params(@ref.referral_code) | |
expect(User.first.referral_code).to be_present | |
end | |
it 'adds a point to the referrer' do | |
post :create, params: create_user_params(@ref.referral_code) | |
@ref.reload | |
expect(@ref.points).to eq 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I noticed 2 things:
CreatesPoints#create_point
doesn't save the user after incrementingpoints
. Could that be the cause?@ref = create(:user)
and the newly-created referral User may have an emptyreferral_code
. If that's the case,User.find_by(referral_code: …)
could be returning a different user than the referrer.