2. What is testing?
“Software testing is an investigation conducted to provide
stakeholders with information about the quality of the
product or service under test”
What?!?
It’s writing other bits of code to execute and run over our
codebase to check it works as we want, and provide
information to us when it doesn’t work.
3. Why test?
Many, many reasons….
● Can check that our big, complex programs work the way we want
● Can check software still works every time we make changes
● Can check software still works every time someone else makes changes
● Helps us remember and formalize what “correct behaviour” is
● i.e. permanently document conditions and responses we want
● the list goes on….
4. First “test”
Create a quick ruby file with this code:
require 'pry'
def add_one(number)
number + 1
end
binding.pry
What happens when we call each of the following in Pry:
add_one(3), add_one(4.5), add_one(“one”), add_one(), add_one(nil)
5. First “test” cont’d...
Change the add_one method code to:
def add_one(number = “No number given”)
if number.is_a?(Numeric)
puts “Calculation was successful”
number + 1
else
raise ArgumentError, "Numbers only please"
end
end
Now try: add_one(3), add_one(“one”), add_one(), add_one(nil)
6. Testing in Rails
Can create powerful, automated test suites easily* in Rails.
Ruby/Rails have the Minitest and Rspec frameworks.
These are testing frameworks that interact with our codebase and can run a full
test suite automatically, from a single command line prompt.
By default, every Rails application has three environments: development, test,
and production. For testing, you work inside the test environment, so
development and production data is not compromised.
* “easily” only really applies to developers who know what they’re doing.
7. Test Driven Development
Test Driven Development (TDD) is a software development
process in which the developer writes tests for a specific
feature or behavior first (which obviously then fail initially),
before writing code to pass the tests.
The Michael Hartl Rails Tutorial teaches test driven
development from the beginning.
8. Minitest - ships with Rails
http://guides.rubyonrails.org/testing.html
Rails creates a test folder for you as soon as you create a Rails project.
Use “rake test” in command line to run entire test suite.
Use “rake test test/models/[TEST NAME HERE].rb” to run a specific test, in
this case a test on our model.
9. Example of a Minitest
e.g. testing our todo model:
require 'test_helper'
class TodoTest < ActiveSupport::TestCase
test "todo should not save without item content" do
todo = Todo.new
assert_not todo.save, "Saved the todo without item content"
end
end
https://github.com/benlcollins/testing_experiments/blob/master/todo_testing_app/test/models/todo_test.rb
10. The Rails package at: https://github.com/rspec/rspec-rails
Add the gem to your Gemfile:
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end
Then “bundle install”
Then initialize the spec directory “rails generate rspec:install”
To run tests “bundle exec rspec”
or “bundle exec rspec spec/models/[TEST NAME HERE].rb”
RSpec - popular testing framework
11. Example of RSpec
Example of an RSpec test (same model test as above):
require 'rails_helper'
RSpec.describe Todo, :type => :model do
it "doesn't save new todo with blank item content" do
new_todo = Todo.new
expect(new_todo).not_to be_valid
end
end
https://github.com/benlcollins/testing_experiments/blob/rspec_example/todo_testing_app/spec/models/todo_spec.rb
12. Further resources
Good intro: http://www.codenewbie.org/blogs/testing-your-code
Rails Minitest docs: http://guides.rubyonrails.org/testing.html
RSpec docs: http://rspec.info/
RSpec best practises: http://betterspecs.org/
How we test Rails apps: https://robots.thoughtbot.com/how-we-
test-rails-applications