CSC/ECE 517 Fall 2015/oss E1573 sap

From Expertiza_Wiki
Jump to navigation Jump to search

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.


Why is unit testing required

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

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

Tested Spec Methods

team_Assignment: The methods returns true by default indicating it is a team assignment. The rspec method #team_assignment checks whether the method returns true or not

has_Teams: The method in the assignment model returns true or false depending upon whether the assignement has one or more teams associated with it. The corresponding rspec method #has_teams? checks whether the method returns true when a team has been assigned to it.

has_Topics: The method in the assignment model returns true or false depending upon whether the assignment has one or more topics associated with it. The corresponding rspec method #has_topics? checks whether the method returns true when a team has been assigned to it.

is_wiki_assignment: The method in the assignment model returns true or false depending upon whether the assignment is a wiki assignment or not. It returns true if its wiki_type_id > 1. The corresponding rspec method #is_wiki_assignment validates that the model method returns true when the assignment is a wiki assignment with wiki_type_id >1

is_google_doc: The method in the assignment model returns true or false depending upon whether the assignment is a google document or not. It returns true if its wiki_type_id =4. The corresponding rspec method #is_google_doc validates that the model method returns true when the assignment is a google document with wiki_type_id =4

is_microtask: The method in the assignment model returns true or false depending upon whether the assignment is a microtask or not. It returns true if the microtask variable isassigned true in the assignment. The corresponding rspec method #is_microtask? validates that the model method returns true when the microtask variable is assigned true in the assignment

dynamic_reviewer_assignment: The method in the assignment model checks whether the review_assignment_strategy is set to 'Auto-Selected' or not. It returns true if the review_assignment_strategy value is set to ‘Auto-Selected’. The corresponding rspec method #dynamic_reviewer_assignment? validates that the model method returns true when the review_assignment_strategy is set to 'Auto-Selected'.

candidate_assignment_teams_to_review: The spec method #candidate_assignment_teams_to_review validates whether the model method returns nil when there are no contributors to the assignment in the team

candidate_topics_for_quiz: The corresponding spec method validates whether the model method returns nil if the assignment has no sign up topic

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.

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>

Setting up Rspec and FactoryGirl

GemFile

Add the following lines of code in the gemfile:

source 'https://rubygems.org'

gem 'rspec-rails','~>3.0'
gem 'factory_girl-rails','~>4.0'

Preparing FactoryGirl

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

Add the following lines of code to the RSpec config in spec/spec_helper.rb.

require 'factory_girl_rails'

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

Building Factories

Each factory has a name and a set of attributes. Implicitly, the name of the factory corresponds to the the class, whose test objects are created by default, but it's possible to explicitly specify the class also.

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

require 'spec_helper'

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

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

References

<References/>