- Read and interpret tests
- Resolve errors with the aid of Pry
If you open this lab and run the tests, you'll notice something: everything is broken. Like, broken bad. And guess what? It's on you to fix it!
Keep in mind, this lab is about more than just finding and fixing the errors -
it's about understanding how to look for what those errors are. The instructions
in this lab are intentionally minimal. Use the test results to get started. Take
time to read and interpret the code in each file in the fix_using_tests
folder.
- What do the methods in each file do, or at least, what should they be doing?
- What is stopping them from successfully doing these tasks?
Add binding.pry
into the existing methods to jump into your code and confirm
your suspicions about what is going on. Remember to require 'pry'
at top of
each file you want to pry into.
Once you've required pry
, you can place as many binding.pry
lines as you'd
like. Use this to your advantage. For example, in the first file you need to
fix, fix_using_tests/false_equivalency
, the selection
method has a
conditional statement:
def selection(num)
if num = 1
"YUM YUM MUNCH MUNCH MUNCH"
elsif num = 2
"HAM HAM HAM IN MY TUMMY"
end
end
Test your own assumptions about this code by placing two binding.pry
lines,
one for each potential condition:
def selection(num)
if num = 1
binding.pry
"YUM YUM MUNCH MUNCH MUNCH"
elsif num = 2
binding.pry
"HAM HAM HAM IN MY TUMMY"
end
end
There are two tests that run for this file. The first passes in 1
as the value
for this method. The second passes 2
. Your initial assumption may be that we
should hit the first binding.pry
on the first test. Run learn
, and you
should see that this is correct. Type exit
while in Pry to continue on to the
second test. You might assume that you'll hit the second binding.pry
now,
but you don't! Why is that? Read the provided code carefully.
Sometimes, you may run into a situation where you've added binding.pry
to the
end of a method, but when running learn
, it does not pause. There are two
potential causes of this:
-
This issue can sometimes happen if
binding.pry
is the last line of code in a method like the example below.def selection(num) if num = 1 "YUM YUM MUNCH MUNCH MUNCH" elsif num = 2 "HAM HAM HAM IN MY TUMMY" end binding.pry end
If you've run into this situation, the easiest way to get around this is to add a line of code after
binding.pry
so that it isn't the last line of code that is interpretted.def selection(num) if num = 1 "YUM YUM MUNCH MUNCH MUNCH" elsif num = 2 "HAM HAM HAM IN MY TUMMY" end binding.pry puts 'hello' end
Just make sure to remove this extra code along with
binding.pry
when running the tests again. Otherwise, your will cause the method to return something unexpected. In the example above,puts 'hello'
returnsnil
, which will, in turn, cause theselection
method to returnnil
when called. -
If you've tried the above solution and Pry still does not seem to work, it may be that the method you've placed
binding.pry
in is never actually called. For instance, if you have the following:def runner prompt_user selection(get_user_input) binding.pry end
When you run learn
, Pry will never work. Why? Because the runner
method is
not actually called in the tests! The test file spec/false_equivalency_spec.rb
has the following in it:
describe "false_equivalency" do
it "`selection` returns the correct string based on user input" do
expect(selection(1)).to eq("YUM YUM MUNCH MUNCH MUNCH")
expect(selection(2)).to eq("HAM HAM HAM IN MY TUMMY")
end
end
If you recall from the lesson How Tests Work, each expect
line is a
separate test. These tests are only running the selection
method! All the
other methods in fix_using_tests/false_equivalency.rb
are ignored in this
test.