Lecture 7 - Testing

Guest Lecturer

Francis Chiwunda

Founder of startup Bus in Africa

What do you know about the components of the Rails directory?

  • app/: the home of the MVC
  • config/routes.rb: web requests
  • db/: migrations, schema,and seeds
  • test/: tests

Components of the test directory

tests for controller functionality

test data

tests for helper methods

tests for multiple controller interaction

tests for mailers

tests for models

tests for controller functionality

tests for models

default configuration to run all tests

test data

Fixtures

  • fancy word for sample data
  • populates your test database before your tests run
  • by default "test_helper.rb" loads all your fixtures into the test database before any tests run
  • written in YAML
  • one fixture file per model
  • automatically created with "rails generate model"
  • can use embedded ruby inside

Make 100 Books

<% 100.times do |n| %>
book_<%= n %>:
  id: <%= n %>
  title: <%= "Book #{n}" %>
  author: <%= "Author #{n % 10}" %>
<% end %>

Accessing Fixtures and Running Tests


  • books(:FIXTURE_NAME).ATTRIBUTE
  • for total size, we use Model.count
test "should have same title" do
  assert_equal "Book 3", books(:book_3).title, "Title wrong!"
end

test "should load 100 books" do
  assert_equal(100, Book.count, "Didn't load 100 books")
end
  • rake test : run all tests
  • rake test PATH_TO_TEST_FILE run a specific test
  • rake test:controllers or rake test:models

What it looks like to fail

# Running:

F.

Finished in 0.031153s, 64.1993 runs/s, 64.1993 assertions/s.

  1) Failure:
BookTest#test_should_have_same_title [/PATH_TO_APP/test_app/test/models/book_test.rb:5]:
Title wrong!.
Expected: "Harry Potter"
  Actual: "Book 3"

2 runs, 2 assertions, 1 failures, 0 errors, 0 skips

2 tests

  • Can also show "E" for error 

Unit Testing Models

  • rails generate model creates MODEL_test.rb
  • tests for model-specific functionality (eg. validation)
  • expected behavior with assertions
    • http://guides.rubyonrails.org/testing.html
      • many useful assertions from "minitest", the default testing library used by Rails, and Rails specific assertions
    • can create your own custom assertions!

Functional Testing Controllers

  • rails generate controller creates MODEL_CONTROLLER_test.rb
  • tests actions within a single controller
  • things typically tested for in functional tests:
    • web requests success
    • redirect_to desired page
    • appropriate message displayed
    • etc.
require 'test_helper'

class BooksControllerTest < ActionController::TestCase
  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:books)
  end
end

Simulates GET request on action "index"

Makes sure request worked

Checks that @books was assigned

  • For requests that need params, you can simulate that too:
    • get(:show, {'id' => "11"})

Logistics

  • Project 1 was released, due on Sunday November 2 at 11:59 PM
  • Project 2 is a group project, so start forming teams of up to 4!
    • We will give you time next week during lecture to form groups in class if you need, and to brainstorm ideas with your group

Lab time!

Lecture 7 - Testing

By Rails Decal

Lecture 7 - Testing

  • 1,385