Skip to content

Instantly share code, notes, and snippets.

@kvirani
Last active February 7, 2019 18:30
Show Gist options
  • Save kvirani/7108a35a180457a0ff01 to your computer and use it in GitHub Desktop.
Save kvirani/7108a35a180457a0ff01 to your computer and use it in GitHub Desktop.
WE1 - Practice Debugging with RSpec supporting us.

Practice Debugging With RSpec

Debugging... Ahh, the life blood of developers. Okay let's get straight to work!

This gist contains individual exercises (one per file) for you to work through, in order. Fork and clone it and run through each file (in order) and fix the bugs in the code.

For now, we are keeping both the logic code and the test code for each exercise in one file. This is for simplicity since the code is minimal. Normally though, rspec tests would be in a separate file.

To run a particular exercise, run the file using the rspec command. Example:

rspec 1_hello_world.rb

Important Note: Don't attempt to modify/fix the test (Rspec) code. The bugs in these challenges are always in the logic code, not the test code. Consider the test code documentation for defining how the logic code is expected to behave.

### SETUP
require 'rspec'
RSpec.configure do |config|
config.color_enabled = true
end
### LOGIC (fix me)
def hello(who)
"hello #{who}"
end
### TEST CODE (don't touch me)
describe "#hello" do
it "returns 'hello world!' when 'world' is passed in" do
result = hello('world')
result.should eq('hello world!')
end
end
### SETUP
require 'rspec'
RSpec.configure do |config|
config.color_enabled = true
end
### LOGIC (fix me)
# Returns the average of all the numbers in the array
def average(numbers)
sum = 0
numbers.each do |n|
sum += n
end
sum / numbers.size
end
### TEST CODE (don't touch me)
describe "#average" do
it "returns nil for empty array" do
result = average([])
result.should eq(0)
end
it "returns nil when nil is passed in" do
result = average(nil)
result.should be_nil
end
it "returns 4 for 3,4,5" do
result = average([3,4,5])
result.should eq(4)
end
it "can handle numbers represented as strings" do
result = average([10,'20',30])
result.should eq(20)
end
it "can handle floats" do
result = average([1.0,1.5,2.0])
result.should eq(1.5)
end
end
@Andsbf
Copy link

Andsbf commented Mar 9, 2015

Hi Khurram Virani, I'm a Lighthouse Labs student...I would like to suggest you a change in the "1_hello_world.rb" file.

Apparently the test code has a typo on this line:

... when 'world!' is passed i...
... when 'world' is passed i... <=CORRECTED - without the exclamation mark, so it would match what is will be tested on the next line of code:

result = hello('world') <= without exclamation mark

Because if I input 'world!' (with exclamtion mark) in the #hellow method it will return me "hello world!" when it should return me "hello word!!"(2 exclamation marks).

I hope i'm clear in the explanation, if not please let me know!

@kvirani
Copy link
Author

kvirani commented Mar 10, 2015

Updated, thanks @Andsbf

@raySavignone
Copy link

Hi KV ,

I think there might be another typo in the arrays file .

the test that reads : "returns nil for empty array" , is expecting result to eq(0) , if it should return nil why the 0 value?

@johnnyji
Copy link

johnnyji commented Apr 5, 2015

@kvirani

Hey Khurram,

There have been some updates:

RSpec.configure do |config| config.color_enabled = true end no longer works, the updated version is:

RSpec.configure do |config| config.color = true end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment