-
Create the
filename.rb
file for which you’d like to create afilename_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 continuallyrequire
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 yourspec_helper.rb
so 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.rb
within your spec file. In new versions of rspec,.rspec
which is generated with the commandrspec --init
requiresspec_helper.rb
for all the specs you’d add in the future.
Last active
April 28, 2023 23:16
-
-
Save boddhisattva/f0fcd0c0045fad87ef39 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