Here are three challenges to help you get started with Rspec. Before starting, be sure that you have Rspec installed with gem install rspec.
We want to setup a simple application that will allow a new user to be created. An instance of User should be able to call a greeter method that returns the message, "Hello, name of new user!". Using Rspec, test drive the following expectations. ONLY WRITE ENOUGH CODE TO CORRECT THE CURRENT ERROR. You should be starting with just a spec file. Test driven means tests before code!
- You can create a new instance of User and access the user's name
- An instance of User can call a 'greet' method that returns a welcome message with the user's name
Now we want to wrap a unit test around an existing feature, simulating taking over an existing project.
- Create a new project and clone the gist we will use with the command:
git clone https://gist.github.com/6250468.git lib - Setup a spec to ensure that calling the method 'fizzbuzzer' on a new instance of FizzBuzz responds properly for the numbers 9, 10, 11 and 15. This should be one single spec, with four expectations.
Using the same, now corrected FizzBuzz project, test drive a feature that will allow you to output the results of the 'fizzbuzzer' method to json. (Hint: use the errors as a learning tool to help you drive out expectations you may not know the exact value of.) Again, remember to drive the feature out with specs first!