Difference between revisions of "Emergent Web Technologies Spring 2009 Class 12"
m |
m |
||
Line 7: | Line 7: | ||
− | '''1''' First, open up the Instant Rails console and create an application for testing by running the following commands | + | '''1)''' First, open up the Instant Rails console and create an application for testing by running the following commands |
rails testing_app | rails testing_app | ||
cd testing_app | cd testing_app | ||
Line 17: | Line 17: | ||
rake db:test:load | rake db:test:load | ||
− | '''2''' The scaffold command automatically generated some tests for your application. We'll be looking at the unit tests for the '''location''' model. | + | '''2)''' The scaffold command automatically generated some tests for your application. We'll be looking at the unit tests for the '''location''' model. |
First, make sure that the generated tests are working. This will verify that your scaffolded project has been set up correctly. | First, make sure that the generated tests are working. This will verify that your scaffolded project has been set up correctly. | ||
Line 30: | Line 30: | ||
1 tests, 1 assertions, 0 failures, 0 errors | 1 tests, 1 assertions, 0 failures, 0 errors | ||
− | '''3''' Now, open the generated test so that you can edit to actually your location model. You can find the test at the following location | + | '''3)''' Now, open the generated test so that you can edit to actually your location model. You can find the test at the following location |
test/unit/location_test.rb | test/unit/location_test.rb | ||
Line 49: | Line 49: | ||
This simple test will test to make sure that the location address property actually works. Normally, you wouldn't need to test something simple like this, but we'll just work with the simplest examples for now. | This simple test will test to make sure that the location address property actually works. Normally, you wouldn't need to test something simple like this, but we'll just work with the simplest examples for now. | ||
− | '''4''' Assume that a simple test looks something like this | + | '''4)''' Assume that a simple test looks something like this |
test "What the test is testing" do | test "What the test is testing" do | ||
a_variable = "A value" | a_variable = "A value" | ||
Line 65: | Line 65: | ||
This activity uses the Screw.Unit JavaScript testing framework. This is a simple and easy to use framework and, despite the name, it actually works pretty well. | This activity uses the Screw.Unit JavaScript testing framework. This is a simple and easy to use framework and, despite the name, it actually works pretty well. | ||
− | '''1''' I have created a test project that you need to download from [http://www.mattephraim.com/ewt/class_12/javascript_tests.zip here]. | + | '''1)''' I have created a test project that you need to download from [http://www.mattephraim.com/ewt/class_12/javascript_tests.zip here]. |
Unzip the file and open the '''index.html''' file in your browser. You should see a test output screen. One of the tests should pass and the other should fail. | Unzip the file and open the '''index.html''' file in your browser. You should see a test output screen. One of the tests should pass and the other should fail. | ||
Line 75: | Line 75: | ||
* the '''tests''' folder contains a file called '''test.js''' that contains the tests that we will be using | * the '''tests''' folder contains a file called '''test.js''' that contains the tests that we will be using | ||
− | '''2''' | + | '''2)''' Open the file located at |
+ | tests/tests.js | ||
+ | |||
+ | The test contains the following functions: | ||
+ | * a '''before''' function that sets up the test with a new copy of the person class. | ||
+ | * a test that makes sure the '''setFirstName''' and '''getFirstName''' functions are working correctly. This test should pass. | ||
+ | * a test that makes sure the '''setLastName''' and '''getLastName"'' functions are working correctly. This test is currently failing. | ||
+ | |||
+ | '''3)''' Open the person class located at | ||
+ | lib/person.js | ||
+ | |||
+ | Modify the person class so that the failing test works. You can make sure that all of your tests work by reloading the index.html page in your browser. | ||
+ | |||
+ | '''4)''' Create a new test, similar to the existing tests, that tests the '''setAge''' and '''getAge''' functions. Make sure that the test passes by reloading index.html your browser. | ||
== Activity 3 == | == Activity 3 == | ||
This activity uses the Watir framework to remotely control your browser. | This activity uses the Watir framework to remotely control your browser. |
Revision as of 21:35, 29 April 2009
Introduction
Activity 1
This activity uses the testing framework that comes with a standard Ruby on Rails installation. The Ruby on Rails community (and the Ruby community in general) are very enthusiastic about testing.
1) First, open up the Instant Rails console and create an application for testing by running the following commands
rails testing_app cd testing_app ruby script/generate scaffold Location name:string address:string city:string state:string zip:string rake db:migrate
Before testing, you'll also need to set up the testing database by running the following commands
rake db:migrate rake db:test:load
2) The scaffold command automatically generated some tests for your application. We'll be looking at the unit tests for the location model.
First, make sure that the generated tests are working. This will verify that your scaffolded project has been set up correctly.
cd test ruby unit/location_test.rb
If everything worked correctly, you should see a message that looks like this
Started . Finished in 0.25 seconds. 1 tests, 1 assertions, 0 failures, 0 errors
3) Now, open the generated test so that you can edit to actually your location model. You can find the test at the following location
test/unit/location_test.rb
Modify the location test so it looks like this
require 'test_helper' class LocationTest < ActiveSupport::TestCase test "address works" do address = "11 E Adams" location = Location.new location.address = address assert_equal location.address, address end end
This simple test will test to make sure that the location address property actually works. Normally, you wouldn't need to test something simple like this, but we'll just work with the simplest examples for now.
4) Assume that a simple test looks something like this
test "What the test is testing" do a_variable = "A value"
location = Location.new location.property = a_variable assert_equal location.property, a_variable end
Use this pattern to write a test for each of the location's properties: name, city, state and zip. Then run the test file to make sure that all of your tests pass.
Activity 2
This activity uses the Screw.Unit JavaScript testing framework. This is a simple and easy to use framework and, despite the name, it actually works pretty well.
1) I have created a test project that you need to download from here.
Unzip the file and open the index.html file in your browser. You should see a test output screen. One of the tests should pass and the other should fail.
The test project is broken up into several folders:
- the lib folder has a file called Person.js that represents the person class that you will be testing
- the screwunit folder contains the files from the Screw.Unit framework
- the tests folder contains a file called test.js that contains the tests that we will be using
2) Open the file located at
tests/tests.js
The test contains the following functions:
- a before function that sets up the test with a new copy of the person class.
- a test that makes sure the setFirstName and getFirstName functions are working correctly. This test should pass.
- a test that makes sure the setLastName' and getLastName" functions are working correctly. This test is currently failing.
3) Open the person class located at
lib/person.js
Modify the person class so that the failing test works. You can make sure that all of your tests work by reloading the index.html page in your browser.
4) Create a new test, similar to the existing tests, that tests the setAge and getAge functions. Make sure that the test passes by reloading index.html your browser.
Activity 3
This activity uses the Watir framework to remotely control your browser.