CSC/ECE 517 Fall 2012/ch1b 1w39 sn: Difference between revisions
No edit summary |
No edit summary |
||
Line 35: | Line 35: | ||
p.title = 'General title' | p.title = 'General title' | ||
p.content = 'A new content' | p.content = 'A new content' | ||
p.email = ' | p.email = 'Azrael@ncsu.edu' | ||
#place an assert so as to find out whether this statement is valid or not | #place an assert so as to find out whether this statement is valid or not | ||
assert p.valid? | assert p.valid? | ||
Line 43: | Line 43: | ||
</pre> | </pre> | ||
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". |
Revision as of 07:47, 28 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".