Download presentation
Presentation is loading. Please wait.
Published byDarcy Holland Modified over 9 years ago
1
Testing Carol Wolf Computer Science
2
Testing built into Rails Rails comes with three databases. development test production The test database does not contain your important data. It can be filled and emptied without causing problems. Run with rake test. Described in config/database.yml yml is short for yaml yaml stands for yaml ain’t markup language yaml is simpler than xml It shares the descriptive purpose of xml An example is on the next page.
3
development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000
4
config/database.yml for MySql development: adapter: mysql2 encoding: utf8 database: name_development pool: 5 username: root password: mypassword socket: /tmp/mysql.sock
5
test folder Each application has a test folder. Sub folders fixtures – used for sample data functional – tests the controller integration – tests interacting controllers performance – benching and profiling unit – tests for models The scaffold command populates all the sub folders except for integration. An example is on the next slide.
6
fixtures/books.yml # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html one: isbn: MyString author: MyString title: MyString two: isbn: MyString author: MyString title: MyString
7
Fixtures When testing with rake test Empties test database Loads fixtures data Puts fixtures data in a variable for saving Fixtures are in the form of hashes Can write: books(:Dickens).title Fixtures can include ERB Specific tests can be run using rake test:fixtures, rake test:functionals, etc.
8
Unit Tests Unit tests are used for models, i.e. database tables. They contain assertions assert book.invalid? assert book.errors[:isbn].any? The names in quotes will be replaced by names with underscores. require 'test_helper' class BookTest < ActiveSupport::TestCase # Replace this with your real tests. test "the truth" do assert true end
9
Replace book_test.rb with following: require 'test_helper' class BookTest < ActiveSupport::TestCase # Replace this with your real tests. test “book attributes may not be empty" do book = Book.new assert book.invalid? assert book.errors[:isbn].any? assert book.errors[:author].any? assert book.errors[:title].any? end
10
Functional tests Used to test controllers book_controller_test.rb contains tests for each method in the book controller Below the test for the create method. Its parameters are sent with a post method test "should create book" do assert_difference(‘Book.count') do post :create, :book => @book.attributes end assert_redirected_to book_path(assigns(:book)) end
11
Our book_controller_test.rb test "should create book" do assert_difference('Book.count') do post :create, :book => { :isbn => '2345', :title => 'Emma', :author => 'Austen‘ } end assert_redirected_to book_path(assigns(:book)) end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.