When we run RSpec we get failing (Red) results. To pass these failing tests we can add the method push to our SLL like so.
single_linked_list.rb
Now when we run our test the results will be passing (Green). Writing your test before you write your code is Test Driven Development. Your goal is to write your test and then to write the minimal amount of code to satisfy the test. Now that the tests for our push method are passing we should write tests for our pop method.
single_linked_list_spec.rb
Our test for pop will of course fail and thus we should update our code so that they pass.
single_linked_list.rb
We now have passing tests for both push and pop. Still we should add another test using multiple objects since a stack must handle multiple
objects.
single_linked_list_spec.rb (exerpt)
Ooops our new test is failing! We need to go back and fix our push method to handle linking these objects. Which is simple enough as shown below.
single_linked_list.rb
All our tests are now passing and we are done with our stack for today.
tags: Algorithm - Algorithmathon - RSpec - Ruby - Single Linked List - Stack