Testing in Rails 3.x
-
Upload
nelinger -
Category
Technology
-
view
1.089 -
download
2
description
Transcript of Testing in Rails 3.x
Test Automation with RoR for dummies
Aug, 2011Eran Nelinger, SAP [email protected]
04/12/2023
Why Test Automation
Rails Testing Theory
Deep dive into ‘Test’ directory
Other automation investment tips
04/12/2023
Why Test Automation
Rails Testing Theory
Deep dive into ‘Test’ directory
Other automation investment tips
04/12/2023
04/12/2023
04/12/2023
Life’s too short for manual testing…
04/12/2023
Rails make it super-easy for us to write tests (Almost everything we need is already in place with scaffolding)Machine can better remember expected output
04/12/2023
Why Test Automation
Rails Testing Theory
Deep dive into ‘Test’ directory
Other automation investment tips
04/12/2023
Rails applications typically interacts heavily with databasesIf we would like to write efficient tests, they’ll need to interact with databases
04/12/2023
We would like to be able to run our test in such environment where messing our data does not really make a differenceOn the other hand we want to rebuild our data each time before a test run
04/12/2023
The Three Environments approach allows us to set up and interact with test data without any danger of your tests altering data from your production environment
04/12/2023
test/fixtures:•Just a fancy word for ‘sample-data’•Populate our test-schema with content each time before a test runs•Database independent• Supports YAML as well as CSV
04/12/2023
04/12/2023
04/12/2023
test/unit:•Unit-tests in rails are model-tests•Logical assertions of our application’s business logic•Should include at list one test method for every method in the model
04/12/2023
test/unit/helpers:•Any method that might be used in more then one test-case
04/12/2023
test/unit/test_helper.rb:•Define ActiveSupport::TestCase class which is the base class for each test (not only unit)•Requires by each of the test files•Switch to RAILS_ENV = “test”•Brings us assertions from test test/unit
04/12/2023
test/functional•ActionController::TestCase•Controller specific tests was the web request successful? was the user redirected to the
right page? was the user successfully
authenticated? was the correct object stored in
the response template? was the appropriate message
displayed to the user in the view?
04/12/2023
test/integration•ActionController::IntegrationTest•Interaction among number of controllers•Generally used to test important flows•Fixtures -> to be included explicitly
04/12/2023
test/performance•ActionDispatch::PerformanceTest•Runs a code profiler of each test method•Flat and graph_html output is written by defauls
04/12/2023
Before running the tests:1. $ rake db:migrate
runs any pending migrations on the development environment and updatesdb/schema.rb
2. $ rake db:test:loadrecreates the test database
from the current db/schema.rb
04/12/2023
Assertions are the working bees of testingActually performs a check to see if expected==actual
04/12/2023
Assertions are the working bees of testingActually performs a check to see if expected==actual
Assertion Purpose
assert( boolean, [msg] ) Ensures that the object/expression is true.
assert_equal( obj1, obj2, [msg] ) Ensures that obj1 == obj2 is true.
assert_not_equal( obj1, obj2, [msg] ) Ensures that obj1 == obj2 is false.
assert_same( obj1, obj2, [msg] ) Ensures that obj1.equal?(obj2) is true.
assert_not_same( obj1, obj2, [msg] ) Ensures that obj1.equal?(obj2) is false.
assert_nil( obj, [msg] ) Ensures that obj.nil? is true.assert_not_nil( obj, [msg] ) Ensures that obj.nil? is false.
assert_match( regexp, string, [msg] ) Ensures that a string matches the regular expression.
assert_no_match( regexp, string, [msg] ) Ensures that a string doesn’t matches the regular expression.
assert_in_delta( expecting, actual, delta, [msg] ) Ensures that the numbers expecting and actual
04/12/2023
Rails specific assertions
Assertion Purpose
assert_valid(record) Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
assert_difference(expressions, difference = 1, message = nil) {...}
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
assert_no_difference(expressions, message = nil, &block) Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
assert_response(type, message = nil)Asserts that the response comes with a specific status code. You can specify :success to indicate 200, :redirect to indicate 300-399, :missing to indicate 404, or :error to match the 500-599 range
assert_redirected_to(options = {}, message=nil)
Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such thatassert_redirected_to(:controller => "weblog")will also match the redirection ofredirect_to(:controller => "weblog", :action => "show") and so on.
04/12/2023
Why Test Automation
Rails Testing Theory
Deep dive into ‘Test’ directory (Model testing)
Other automation investment tips
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
Why Test Automation
Rails Testing Theory
Deep dive into ‘Test’ directory (Controller testing)
Other automation investment tips
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
Why Test Automation
Rails Testing Theory
Deep dive into ‘Test’ directory (View testing)
Other automation investment tips
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
04/12/2023
http://guides.rubyonrails.org/testing.htmlhttp://guides.rubyonrails.org/http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.htmlhttp://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.htmlhttp://api.rubyonrails.org/classes/ActiveSupport/TestCase.htmlhttp://api.rubyonrails.org/classes/ActionController/TestCase.htmlhttp://rdoc.info/github/rails/rails/master/ActionDispatch/IntegrationTesthttp://watir.com/http://pivotal.github.com/jasmine/http://railscasts.com/episodes/261-testing-javascript-with-jasmine
04/12/2023