-
Create the
filename.rbfile for which you’d like to create afilename_spec.rb -
Run the command
rspec --initfrom the same working directory as to where you have filename.rb -
the above command will create a
.rspecfile for you -
It will create a new spec directory within which you need to place your specs
-
It will create
spec_helper.rbwhere you need continuallyrequirethe ruby files that you would want to write specs for -
Create the
filename_spec.rbwithin the spec directory -
[optional] Edit the
.rspecfile 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.rbwithin yourspec_helper.rbso that rspec knows against which reference class(herefilename.rb) the specs are written and thereby the specs(in thefilename_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.rbwithin your spec file. In new versions of rspec,.rspecwhich is generated with the commandrspec --initrequiresspec_helper.rbfor all the specs you’d add in the future.
-
-
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment