Week 3 - Test Driven Development Part 2

Week 3 - Test Driven Development Part 2

·

3 min read

Feature Test

Tests that all the different parts of the system work together well.
Tests that implement such user stories are feature tests.
These tests tell stories per test, they explain the features that our app has, and that our users care about.

Stack trace

Is a representation of the stack at a specific moment while running your programme. When there is an error Ruby will print the exception along with the stack trace to the console. The stack trace will show the stack in a human-readable format with each method’s location in your code to help you pinpoint where the exception came from.

3 parts to error

NameError: uninitialized constant DockingStation
  from (irb):1
  from /Users/username/.rvm/rubies/ruby-2.2.2/bin/irb:11:in '<main>'

Name of the error – ‘NameError’
Path where it happened - at the top of the stack ‘1: from (irb): 1’
Line number where it occurred – at the stop of the stack shows ‘(irb) : 1’ which happened at line number 1.

If we still can’t figure out the errors, we can look at Ruby’s documentation on exception which is another word for errors.

https://ruby-doc.org/core-2.2.0/Exception.html

Initialise RSpec

Install rspec to my machine gem install rspec

Initialise rspec rspec —init

Creates an rspec file .rspec - this is a system file as it has a ., we can reveal it by using ls -a

Creates a directory called spec with a file inside called spec_helper.rb

The Single Responsibility Principle

It helps keep classes and methods small and maintainable. In addition to keeping classes small and focused it also makes them easier to understand.
Generally, it is said that anything that gives a class a reason to change can be viewed as a responsibility.

 def dock(bike)
      fail 'Docking station full' if @bikes.count >= 20
      @bikes << bike
  end

This method is doing two things:

  1. Defining the capacity of the docking station (20), and
  2. Docking a bike.

As it stands, dock does not have a Single Responsibility.

We will use private methods to extract method responsibilities to other methods.

  def dock(bike)
    if full?
      raise "Docking Station is full"
    else
      @bikes << bike
    end
  end

  private
  def full? 
    return @bikes.length >= DEFAULT_CAPACITY
  end