CSC/ECE 517 Fall 2015/E1573 sap: Difference between revisions
(Created page with "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 pr...") |
No edit summary |
||
Line 1: | Line 1: | ||
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. | == '''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== | ==Goals== | ||
Line 14: | Line 16: | ||
==Factory Girl== | ==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. | 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: | |||
<code><nowiki>source 'https://rubygems.org'</nowiki> | |||
gem 'rspec-rails','~>3.0' | |||
gem 'factory_girl-rails','~>4.0'</code> | |||
=== 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. | |||
<code>require 'factory_girl_rails' | |||
RSpec.configure do |config| | |||
config.include FactoryGirl::Syntax::Methods | |||
end</code> | |||
== | === Building Factories === | ||
Each factory has a name and a set of attributes. | 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: | This will create the objects of the User class: | ||
Line 39: | Line 52: | ||
end | end | ||
This will use the User class (Admin would have been created): | This will use the User class (Admin would have been created): | ||
factory :admin ''class: User'' do | factory :admin '''class: User''' do | ||
sequence(:name) { |n| "NewName #{n}" } | sequence(:name) { |n| "NewName #{n}" } | ||
fullname {"test_user"} | fullname {"test_user"} | ||
Line 57: | Line 70: | ||
microtask {true} | microtask {true} | ||
review_assignment_strategy {'Auto-Selected'} | review_assignment_strategy {'Auto-Selected'} | ||
''association :instructor, factory: :user | '''''association :instructor, factory: :user''''' | ||
association :wiki_type, factory: : | '''association :wiki_type, factory: :wiki_typ'''e'' | ||
end | end | ||
==Creating specs == | ===Creating specs === | ||
*Create a assignment_spec.rb file in spec/models folder | *Create a assignment_spec.rb file in spec/models folder | ||
An example of a test case is | An example of a test case is | ||
<code>require 'spec_helper' | |||
describe "Team_Assignment" do | |||
describe "Team_Assignment" do | |||
it "checks team assignment should be true" do | |||
assign=FactoryGirl.create(:assignment) | assign=FactoryGirl.create(:assignment) | ||
res=assign.team_assignment | res=assign.team_assignment | ||
expect(res).to be true | expect(res).to be true | ||
end | end</code> | ||
==Running RSpec== | ===Running RSpec=== | ||
$ rspec spec/models/assignment_spec.rb | $ 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) | Finished in 1.11 seconds (files took 14.47 seconds to load) | ||
14 examples, 0 failures | 14 examples, 0 failures | ||
==References== | ==References== |
Revision as of 12:50, 28 October 2015
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. <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 this 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