User:Sjolly92/sandbox/OSS E1573

E1573 Unit Test for Assignment model
Assignment.rb is a model in Expertiza -a ruby rails application, that handles the various assignments assigned by the instructors to the teams during their coursework. The OSS project "Unit Test for Assignment model" tests the relationships, validation and scope of the assignment model using RSpec and Factory Girl.

Goals

 * Write specs for major instance methods of assignment.rb model
 * Create test data by writing factories and use the test data to test the model with rspec

Why is unit testing required

 * Visibility and reporting
 * Control and correction
 * Efficiency and speed
 * Planning and predictability
 * Customer satisfaction

Factory Girl
Factory Girl is a fixture replacement which has easy-to-use definition syntax. factory_girl_rails provided Rails integration for Factory Girl. We define a factory to create objects which will be used for testing.

RSpec
RSpec is used for Test Driven Development. It is a Domain Specific Language, also called DSL, used specifically for Ruby Rails projects. It is used for describing the expected behavior of a system with executable examples. RSpec code is similar to Ruby code and it leverages Ruby features and syntax so as to make up a “mini-language” focused on the job of testing. It is commonly used for Unit Testing and Functional testing.

GemFile
Add the following lines of code in the gemfile:

Preparing FactoryGirl
FactoryGirl is a Ruby Gem that mocks certain classes for automated tests. We will use FactoryGirl to mock nodes and lists.

Add this to the RSpec config in spec/spec_helper.rb.

Building Factories
Each factory has a name and a set of attributes. The name is used to guess the class of the object by default, but it's possible to explicitly specify

This will create the objects of the User class: factory :user do        sequence(:name) { |n| "NewName #{n}" } fullname {"test_user"} email {"sjolly@ncsu.edu"} parent_id =1 is_new_user=true end This will use the User class (Admin would have been created): factory :admin class: User do       sequence(:name) { |n| "NewName #{n}" } fullname {"test_user"} email {"sjolly@ncsu.edu"} parent_id =1 is_new_user=true end

It's possible to set up associations within factories. If the factory name is the same as the association name, the factory name can be left out, as given below :

Factory for Assignment having associations with instructor and wiki_type:

factory :assignment_without_name, class: Assignment do       name {} submitter_count {3} is_coding_assignment {true} microtask {true} review_assignment_strategy {'Auto-Selected'} association :instructor, factory: :user association :wiki_type, factory: :wiki_type'' end

Creating specs

 * Create a assignment_spec.rb file in spec/models folder

An example of a test case is

Running RSpec
$ rspec spec/models/assignment_spec.rb You should be able to run tests successfully and see the following message: Finished in 1.11 seconds (files took 14.47 seconds to load) 14 examples, 0 failures