You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is using instance_variable_set a code smell, when used only in testing? For example:
Is it better to have @guess as a parameter, to be able to easily create a new instance of game for tests subject(:game) { described_class.new(0, 9, 4) }
def initialize(min, max, guess = nil)
@min = min
@max = max
@guess = guess
end
Or is it better to have less arguments and to use game.instance_variable_set(:@guess, 4) in the few tests that require a value for @guess?
def initialize(min, max)
@min = min
@max = max
@guess = nil
end
@rlmoser99 yeah I believe that its a code smell and your solution of passing the param in during initialisation is how it should be done. It fits nicely with the open closed principle. Having to use instance_variable_set seems like a work around.
Activity
KevinMulhern commentedon Oct 30, 2020
@rlmoser99 yeah I believe that its a code smell and your solution of passing the param in during initialisation is how it should be done. It fits nicely with the open closed principle. Having to use instance_variable_set seems like a work around.
rlmoser99 commentedon Oct 31, 2020
Awesome! Thanks for your input! It is great to know with confidence that we should not be using
instance_variable_setin testing.Implement solution for Assignment TheOdinProject#1 in 15a_binary_game…
Implement solution for Assignment TheOdinProject#1 in 16a_caesar_brea…
Ex. 13 Assignment TheOdinProject#1 done
Complete assignment TheOdinProject#1
Complete assignment TheOdinProject#1 and make_guess method
Complete assignment TheOdinProject#1
Complete assignment TheOdinProject#1 & TheOdinProject#2
Complete assignment method TheOdinProject#1
Complete 15a assignment method TheOdinProject#1, partial 2
Complete 15a assignment method TheOdinProject#1, partial 2
Complete 16a assignment method TheOdinProject#1 & TheOdinProject#2
Complete assignment TheOdinProject#1 spec/13_input_output_spec.rb
Write 1st failing test method TheOdinProject#1 spec/14_find_number_sp…
Make pass 1st failing test method TheOdinProject#1 spec/14_find_numbe…
Make pass 2st failing test method TheOdinProject#1 spec/14_find_numbe…
Make pass 3rd 4th 5th tests method TheOdinProject#1 spec/14_find_numb…
Refactor method TheOdinProject#1 tests spec/14_find_number_spec.rb
Complete assignment TheOdinProject#1 spec/15a_binary_game_spec.rb
Complete assignment TheOdinProject#1 spec/16a_caesar_breaker_spec.rb
Complete binary game assignment TheOdinProject#1
Complete ceasar breaker assignments TheOdinProject#1-TheOdinProject#4