Jim's
Tutorials

Fall 2018
course
site

Testing Week 6

I made a thing

So I finally am getting to the point in ruby when I can make tests and kind of understand what they do.I read this documentation about testing with ruby. I made one that I knew would fail I'll talk about it below (in "analysis"):


require_relative './code'

describe 'plus_one' do
it 'should be one plus the number given' do
result = plus_one(0)
expect(result).to eq(1)

end
end

OUTPUT:

method-testing-and-test-driven-development git:(master) ✗ rspec code.rb
FF

Failures:

  1) plus_one should be one plus the number given
     Failure/Error: result = plus_one(0)

     NoMethodError:
       undefined method `plus_one' for #<RSpec::ExampleGroups::PlusOne:0x007fc2cb22b9c0>
     # ./code.rb:5:in `block (2 levels) in <top (required)>'

  2) plus_one should be one plus the number given
     Failure/Error: result = plus_one(0)

     NoMethodError:
       undefined method `plus_one' for #<RSpec::ExampleGroups::PlusOne_2:0x007fc2cb229210>
     # ./code.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.00686 seconds (files took 0.53537 seconds to load)
2 examples, 2 failures

Failed examples:

rspec './code.rb[1:1]' # plus_one should be one plus the number given
rspec './code.rb[2:1]' # plus_one should be one plus the number given

...And I want to talk about this for a second to wrap my mind around it. Tell me if I'm wrong.

Analysis of the Test

So this is a test for a method called "plus one" that takes in a number as an argument and returns a number that is one higher than the one given.

the 'require relative' makes the code in 'code.rb' available for the test.

the 'describe" method takes in a string that describes what's being tested. in this case it's the 'plus one' method, so its in quotes after the 'describe'.

inside the block of the describe method there should be one or more calls to the 'it' method.

the 'it' method takes in a string that describes what the expected outcome of the test should be. So I expect that the 'plus_one' method will return a number one higher than the one given.

Inside the 'it' block there needs to be code that is doing the testing.

this test is comparing that what is given to 'expect' is 'eq' to a particular value.

in this case I should expect the 'result' of 'plus_one' when passed a value of '0' to be '1'.

So the argument passed to 'expect' is the result value of 'plus_one(0)', and the argument given o 'eq' is '1'.

Analysis of the output

So, I got a 'nomethoderror' because I didin't define the 'plus_one' method yet. So I'll do it now:

def plus_one
end

require_relative './code'

describe 'plus_one' do
it 'should be one plus the number given' do
result = plus_one(0)
expect(result).to eq(1)

end
end

OUTPUT:

method-testing-and-test-driven-development git:(master) ✗ rspec code.rb
FF

Failures:

  1) plus_one should be one plus the number given
     Failure/Error:
       def plus_one
       end

     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # ./code.rb:1:in `plus_one'
     # ./code.rb:8:in `block (2 levels) in <top (required)>'

  2) plus_one should be one plus the number given
     Failure/Error:
       def plus_one
       end

     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # ./code.rb:1:in `plus_one'
     # ./code.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.00604 seconds (files took 0.19751 seconds to load)
2 examples, 2 failures

Failed examples:

rspec './code.rb[1:1]' # plus_one should be one plus the number given
rspec './code.rb[2:1]' # plus_one should be one plus the number given

Second Analysis of Output

So I have an 'ArgumentError' in the code.rb file because it doesn't take in any arguments, but I tried to give it an argument of '0'...

It seems I've been having to take an iterative approach to writing these things. I need to clear the 'ArgumentError' now so let me add an argument to my 'plus_one' method

def plus_one(number)
end

OUTPUT:

method-testing-and-test-driven-development git:(master) ✗ rspec code.rb
FF

Failures:

  1) plus_one should be one plus the number given
     Failure/Error: expect(result).to eq(1)

       expected: 1
            got: nil

       (compared using ==)
     # ./code.rb:9:in `block (2 levels) in <top (required)>'

  2) plus_one should be one plus the number given
     Failure/Error: expect(result).to eq(1)

       expected: 1
            got: nil

       (compared using ==)
     # ./code.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.03225 seconds (files took 0.25782 seconds to load)
2 examples, 2 failures

Failed examples:

rspec './code.rb[1:1]' # plus_one should be one plus the number given
rspec './code.rb[2:1]' # plus_one should be one plus the number given

Analysis of third output

OK so ... great I have another error and this one says.. wait... ok so no errors were raised during the execution of the code but the test is still failing.

it seems like it's because the test did not get the expected result of '1' when '0' was passed to the method. I think that's what the 'nil' message means, i thought the return value of 'plus_one' with '0' as an argument would return '1' but the method returned 'nil'.

I'll finish implementing the method.

def plus_one(number)
  result = number + 1
  return result
end

OUTPUT:

method-testing-and-test-driven-development git:(master) ✗ rspec code.rb
..

Finished in 0.00798 seconds (files took 0.19368 seconds to load)
2 examples, 0 failures

Analysis of fourth output

OK good it worked. my first deep dive into test driven development and writing code so tests will pass vs. doing it the other way around. yeah, so writing tests for methods before defining the implementation of the method.

attachments [paper clip]

  last modified size
TXT golfclubs.txt Wed Dec 04 2024 07:14 pm 192B
TXT jims_golf.rb Wed Dec 04 2024 07:14 pm 1.2K