CSC/ECE 517 Fall 2012/ch1b 1w39 sn: Difference between revisions
No edit summary |
No edit summary |
||
Line 50: | Line 50: | ||
</pre> | </pre> | ||
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in <ref>http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing</ref> | This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in <ref>http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing</ref> | ||
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder->Select Run->All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].If you are using command line then you can use the following options | After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder->Select Run->All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]]. | ||
If you are using command line then you can use the following options | |||
<pre> | <pre> | ||
ruby -Itest test/unit/post_test.rb | ruby -Itest test/unit/post_test.rb |
Revision as of 05:59, 30 September 2012
Lecture 10 - Testing in Rails
Setup test environment in Rails
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec<ref>http://rspec.info/</ref>,Cucumber(for behavior driven development),Shoulda <ref>https://github.com/thoughtbot/shoulda#readme</ref>
Initial setup:
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this
require 'test_helper' class PostTest < ActiveSupport::TestCase # Replace this with your real tests. test "the truth" do assert true end end
Now if we wanted to add real tests to it then let us take two scenarios 1.Post with empty entries. 2.Post with actual entries The code for these two test cases would look something like this
require 'test_helper' class PostTest < ActiveSupport::TestCase test "Post new empty" do p = Post.new assert !p.save, "Saved post without title, content, user, or category" assert p.invalid? end test "Post new correct" do p = Post.new #Post has following fields title,email,content p.title = 'General title' p.content = 'A new content' p.email = 'Azrael@ncsu.edu' #place an assert .so as to find out whether this statement is valid or not assert p.valid? end end
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining a test case.The test cases must begin with the name "test". The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.
rake db:migrate rake db:test:load
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in <ref>http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing</ref> After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder->Select Run->All tests in unit.The figure provided below presents a better picture.
If you are using command line then you can use the following options
ruby -Itest test/unit/post_test.rb Loaded suite unit/post_test Started . Finished in 0.023513 seconds. 2 tests, 2 assertions, 0 failures, 0 errors