Skip to content

Instantly share code, notes, and snippets.

@JaisonBrooks
Forked from boddhisattva/getting_started.md
Created December 10, 2015 15:07
Show Gist options
  • Save JaisonBrooks/a421a3749a9e08314f1e to your computer and use it in GitHub Desktop.
Save JaisonBrooks/a421a3749a9e08314f1e to your computer and use it in GitHub Desktop.
Writing a spec from scratch for a plain ruby script(no rails) using TDD

Steps -

  • Create the filename.rb file for which you’d like to create a filename_spec.rb

  • Run the command rspec --init from the same working directory as to where you have filename.rb

  • the above command will create a .rspec file for you

  • It will create a new spec directory within which you need to place your specs

  • It will create spec_helper.rb where you need continually require the ruby files that you would want to write specs for

  • Create the filename_spec.rb within the spec directory

  • [optional] Edit the .rspec file to add the option: --format documentation

  • The above addition gives your running tests more readability as to what they do or what they stand for when they are run.

  • Add a require filename.rb within your spec_helper.rb so that rspec knows against which reference class(here filename.rb) the specs are written and thereby the specs(in the filename_spec.rb) will call the methods and other such things defined in that reference class .

  • Write the spec, see it fail with the command rspec spec/filename_spec.rb

  • Write the minimum code to make it pass. once written run the specs again

  • Once the basic test is green, refactor code, then run your spec again.

  • Refactor tests(some people stop at refactoring code, refactorings tests is a nice thing to do to have more cleaner code within your tests)

  • A side note:- If you’re using an older version of rspec, you might have to do a require spec_helper.rb within your spec file. In new versions of rspec, .rspec which is generated with the command rspec --init requires spec_helper.rb for all the specs you’d add in the future.

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