CSC/ECE 517 Fall 2015/E1573 sap
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. <ref>http://www.rubydoc.info/gems/factory_girl/file/README.md</ref>factory_girl_rails provided Rails integration for Factory Girl. We define a factory to create objects which will be used for testing.
Installation of Factory_Girl_Spec
- Add the following lines of code in the gemfile
gem 'factory_girl-rails','~>4.0'
- run bundle install
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.<ref>https://www.youtube.com/watch?v=oFX1KPNRruA&feature=relmfu</ref>
Installation of RSpec
- Add the following lines of code in the gemfile
gem 'rspec-rails','~>3.0'
- run bundle install
Defining 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
describe "Team_Assignment" do
it "checks team assignment should be true" do assign=FactoryGirl.create(:assignment) res=assign.team_assignment expect(res).to be true end
end
Running RSpec
$ rspec spec/models/assignment_spec.rb
Result of Running RSpec
Finished in 1.11 seconds (files took 14.47 seconds to load) 14 examples, 0 failures