← Back to Upcase

Red-Green-Refactor by Example


(Upcase ) #1
Learn the red-green-refactor loop, the primary principle of TDD. Write tests first, watch them fail, write code incrementally until the tests pass. With passing tests, use the opportunity to refactor.
This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/red-green-refactor-by-example

(Sarah) #2

you have initialize misspelled in your exercise code.


(Geoff Harcourt) #3

Thanks for reporting! We updated it this morning, apologies for the typo.


(Arman Jon Villalobos) #4

I see now that the exercise system (feedback system) is not used here. So it’s safe to assume we post a repo link on the forum to get our code reviewed?


(Chris Toomey) #5

Hey @iamarmanjon, sorry for the confusion here. The exercise included in this video’s notes is intended to provide a structure for you to try out TDD easily, but it is not an exercise in the traditional sense where the solution is the focus.

Instead, the focus is on the process. We wanted to provide a first TDD problem for you to get comfortable with the TDD workflow, taking small steps, working on each failure one at a time, etc. That said, the end result (the final spec suite and implementation) are not expected to be very interesting or different between users. Again, the process is the focus here, not the solution.

As such, we chose to provide this as part of the notes, rather than using the exercise system. That said, step 4 in the trail is a full blown exercise using our Git system where you’re asked to refactor a spec file. In that case the solution is interesting, and thus we use the exercise system.

Sorry for the confusion here, but hopefully the above clears things up. Let us know if you have any more questions.


(Arman Jon Villalobos) #6

thanks @christoomey that clears up a lot.


(neil) #7

require “rspec/autorun”

describe Calculator do
describe “#add” do
it “returns the sum of its two arguments” do
calc = Calculator.new

  expect(calc.add(5, 10)).to eq(15)
end

end
end

Unable to run the first test (3min 52) due to the following error

uby calculator_ruby_tdd.rb
/Users/neilpatel/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such file -- rspec/autorun (LoadError) from /Users/neilpatel/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire’


(Andy Waite) #8

@neilp666 it sounds like you might not have the rspec gem installed. You can run the following at the command line to confirm:

gem search rspec --local


(John Nadeau) #9

Sometimes I just need a simple example like this to get me back into focusing more on the process of TDD. More often than not I drift away from a strict TDD process to a process that sometimes has tests written after the fact.


(Mohamed Ziata) #10

This is my solution for the exercise

Any opinion?


(Mitchell Gould) #11

Here is my attempt. I’m still learning Ruby.


(Chris D'Ambrosio) #12

My solution:


(Askhat Bikmetov) #13

I’m having wrong number of arguments (given 3, expected 0) issue on line 17, which is the initialize method. The initialiser itself, along with whole spec, was copied and pasted from the show notes:

def initialize(first_name:, middle_name: nil, last_name:)
  @first_name = first_name
  @middle_name = middle_name
  @last_name = last_name
end

And actual failure happens when I am creating new Person like so:

person = Person.new("Askhat", "Maratovich", "Bikmetov")

I’m pretty sure the elephant is in the room, but could anyone point me on it?

ruby -v 2.3.1
rspec -v 3.5.3


(Chris Toomey) #14

I believe the issue here is your initialize expects keyword arguments, while your usage is passing positional arguments. So instead of the Person.new as you have it, you’ll want to do:

# note: only breaking across lines for readability
person = Person.new(
  first_name: "Askhat",
  middle_name: "Maratovich",
  last_name: "Bikmetov"
)

Note: We have a Weekly Iteration on Ruby Keyword Arguments if you want to brush up.


(Askhat Bikmetov) #15

You won’t believe how stupid I’m feeling right now, named arguments, obviously. Much obliged!
BTW ruby could be little more articulate on this, instead of wrong number, don’t you think?


(Chris Toomey) #16

I’m always in favor of better error messages :slight_smile:


(Mohnish G J) #17

Here’s my attempt to solving this: https://gist.github.com/boddhisattva/ea7194d749ad891dcc8b76e0d8549e3a . Happy to receive any feedback to improve this further.

Thank you.