RSpec and Capybara (a very basic tutorial)
This is going to be a fairly brief tutorial because Iām moving to England next week and Iām very, very busy... So letās get going!
Iāll be showing you some code in this tutorial to aid your understanding but for the full repository, see this link on my github:Ā https://github.com/rubyandcoffee/rspec-calculator
First of all, letās start with understanding exactly what Test-Driven Development and Behaviour-Driven Development actually is.
Test-Driven Development (āTDDā):
Your app is going to have a bunch of functionalities, but before we code those functionalities, we first need to write a test for the functionality. Of course, it will initially fail because we havenāt actually written the functionality yet. Then, we write the code which adds the functionality, and finally we run the test to ensure that it passes.
Behaviour-Driven Development (āBDDā):
This naturally stems from TDD, but here weāre not only testing what the method or object is, weāre also testing its behaviour - i.e. what it does. Youāll see in the code that the acceptance tests are written as structured sentences, like a story, which puts a real emphasis on exactly what the code does, e.g.Ā āAs a [role] I want [feature] so that [benefit].ā
This may sound confusing, but bear with me.
So where does RSpec and Capybara fit into all of this?
Essentially RSpec is a library/framework for BDD testing. The example sentence I listed before with the ārole, feature and benefitā is where RSpec comes into play. RSpec allows you to write tests for your code (calledĀ āspecsā) which test the expected behaviour for a given method in your program.
I want to write a calculator that sums two numbers together. But first, I have to write a test to check that the calculator sums the two numbers correctly!
So letās have a look at my code for the spec (the test).
https://gist.github.com/rubyandcoffee/d44b9f537acb18bd13889ed4eb4d7a04
First weĀ ādescribeā the class (StringCalculator) and the method (.add), then we give it a context (given two numbers), then a further context (the two numbers being 2, 4) and describe what it is supposed to do (it returns 6) and what we expect it to return (expect 2 + 4 to equal 6).Ā
And then hereās the actual method that this spec would link to:
https://gist.github.com/rubyandcoffee/172ddae4f018333c65081db56925a49f
This is a simple if statement where if the string is empty, return 0, else split the string (into separate numbers), convert to integers, and then sum the numbers together and return it.
Thatās all you need to know as a beginner, and if you want to see the full Rails app then my repo is here:Ā https://github.com/rubyandcoffee/rspec-calculator
Now letās go onto a brief description of Capybara.
Consider this like an add-on, or an extension, to RSpec. It cannot work independently so weāll use it with RSpec.
As I mentioned before, think of RSpec as just a library that allows you to do those sentences within a spec that tests a class.Ā
So now think of Capybara as an extension to that library - an extension that simulates the real user interaction of your application. What do users do? They click buttons, fill in forms, etc... and Capybara lets you test those elements of your app.
If a user wants to sign up, theyāll first have to create an account using their email and password, then theyāll have to be redirected to the appropriate sign in page, where theyāll have to submit their sign in credentials before being led to another page that confirms they have successfully logged in.
This is what Capybara tests for. See the code below:
https://gist.github.com/rubyandcoffee/a47e1ae8797f53d4dd461a2dad3b7d37
And thatās the end of the tutorial! Sorry if it was a bit short, Iām just incredibly busy lately and I promise that Iāll come back and refactor it a little!Ā
In the meantime, please continue to send in your suggestions for future blog tutorials :)
Credit: Some examples for this tutorial used from Wikipedia and semaphoreci.com.