My last PHP tutorial was about PHPUnit tests. Now let’s learn about the more elegant Ruby BDD/TDD framework, named rspec.
I’m using a newly installed Archlinux. First, install sstephenson’s Ruby Version Management software:
Now, build ruby the proper way:
Install bundler, the best way to define GEM requirements in project level:
Create the project directory and some initial files:
Install all required gems by typing: “bundle install”. Now, if you put “rake test” in command line, the test will run with no output (there are no test cases). Let’s create some application logic. I will re-implement my PHP Log class described before. Put this to test/log_spec.rb:
And to lib/log.rb:
As you can see, the rspec test examples have a very readable syntax:
But what is before(:all) and after(:all) does mean? Everything in the do-end block will execute once. Before starting testcase and after all test examples done in the file. You can also use the “:each” when you want to execute anything every time a test example run.
Some other assertions:
Feel free to try everything you want and define more “it must be work, should be true” sentences, run them and create bigger applications with team-players like you. Next, we will see “Cucumber”.