Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created March 17, 2012 23:06
Show Gist options
  • Save jeffreyiacono/2066263 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/2066263 to your computer and use it in GitHub Desktop.
question spec for Proview
require 'spec_helper'
describe Question do
it { should belong_to(:page) }
it { should belong_to(:survey) }
it { should have_many(:answers) }
describe "default scope" do
let(:first_question) { Question.create(seq: 1) }
let(:second_question) { Question.create(seq: 2) }
let(:third_question) { Question.create(seq: 3) }
before { first_question; second_question; third_question }
subject { Question.all }
it { should == [first_question, second_question, third_question] }
end
describe "sorted" do
let(:first_question) { Question.create(seq: 1) }
let(:second_question) { Question.create(seq: 2) }
let(:third_question) { Question.create(seq: 3) }
before { first_question; second_question; third_question }
subject { Question.sorted }
it { should == [first_question, second_question, third_question] }
end
describe "self.types" do
describe "keys" do
subject { Question.types.keys }
it { subject.length.should == 8 }
it { should include('constant_sum') }
it { should include('multi_select') }
it { should include('multi_select_or_none') }
it { should include('instructions') }
it { should include('scale') }
it { should include('selectable_canvas') }
it { should include('slider') }
it { should include('text') }
end
describe "values name key" do
subject { Question.types }
it { subject['constant_sum']['name'].should == 'Constant Sum' }
it { subject['multi_select']['name'].should == 'Checkboxes' }
it { subject['multi_select_or_none']['name'].should == 'Checkboxes or None' }
it { subject['instructions']['name'].should == 'Instructions' }
it { subject['scale']['name'].should == 'Point Scale' }
it { subject['selectable_canvas']['name'].should == 'Selectable Image' }
it { subject['slider']['name'].should == 'Slider' }
it { subject['text']['name'].should == 'Text Box' }
end
describe "values class key" do
subject { Question.types }
it { subject['constant_sum']['class'].should == 'ConstantSum' }
it { subject['multi_select']['class'].should == 'MultiSelect' }
it { subject['multi_select_or_none']['class'].should == 'MultiSelectOrNone' }
it { subject['instructions']['class'].should == 'Instructions' }
it { subject['scale']['class'].should == 'Scale' }
it { subject['selectable_canvas']['class'].should == 'SelectableCanvas' }
it { subject['slider']['class'].should == 'Slider' }
it { subject['text']['class'].should == 'Text' }
end
end
describe "self.new_with_type" do
context "unknown type" do
it { lambda { Question.new_with_type('unknown') }.should raise_error Exception, "Unknown question type" }
end
context "constant sum" do
subject { Question.new_with_type('constant_sum') }
it { should be_instance_of Questions::ConstantSum }
end
context "checkboxes" do
subject { Question.new_with_type('multi_select') }
it { should be_instance_of Questions::MultiSelect }
end
context "checkboxes or none" do
subject { Question.new_with_type('multi_select_or_none') }
it { should be_instance_of Questions::MultiSelectOrNone }
end
context "instructions" do
subject { Question.new_with_type('instructions') }
it { should be_instance_of Questions::Instructions }
end
context "scale" do
subject { Question.new_with_type('scale') }
it { should be_instance_of Questions::Scale }
end
context "selectable canvas" do
subject { Question.new_with_type('selectable_canvas') }
it { should be_instance_of Questions::SelectableCanvas }
end
context "slider" do
subject { Question.new_with_type('slider') }
it { should be_instance_of Questions::Slider }
end
context "text" do
subject { Question.new_with_type('text') }
it { should be_instance_of Questions::Text }
end
end
describe "initialization" do
describe "properties" do
context "when nothing has been set" do
let(:question) { Question.new }
subject { question.properties }
it { should == {} }
end
context "when something has been set" do
let(:question) { Question.new }
before { question.properties['key'] = 'value' }
subject { question.properties }
it { should == {'key' => 'value'} }
end
end
end
describe "save" do
context "when an image is not present" do
let(:question) { Question.new }
before { question.save }
subject { question }
its(:image_width) { should be_nil }
its(:image_height) { should be_nil }
end
context "with an image is present" do
let(:image) { mock "ImageUploader" }
let(:question) { Question.new }
before do
image.should_receive(:geometry).at_least(:twice).and_return([100, 200])
question.stub!(:image).and_return(image)
question.save
end
subject { question }
its(:image_width) { should == 100 }
its(:image_height) { should == 200 }
end
end
describe "number" do
context "with no sequence" do
let(:question) { Question.new(seq: nil) }
subject { question.number }
it { should be_nil }
end
context "with a sequence that is evenly divisible by 1000" do
let(:question) { Question.new(seq: 1000) }
subject { question.number }
it { should == 1 }
end
context "with a sequence that is not evenly divisible by 1000" do
let(:question) { Question.new(seq: 1001) }
subject { question.number }
it { should == 1 }
end
end
describe "last answer" do
let(:question) { Question.new(last_answer: 'something') }
subject { question.last_answer }
it { should == 'something' }
end
describe "export header" do
let(:question) { Question.new(prompt: 'A sample prompt', seq: 1000) }
before { question.properties['options'] = ['option header 1', 'option header 2'] }
subject { question.export_header }
it { should == [['Q1. A sample prompt']] }
end
describe "export_answer" do
let(:question) { Question.new }
subject { question.export_answer }
context "when answers are empty" do
it { should == [''] }
end
context "when one answer is present" do
let(:answer) { Answer.new(value: 'answer value') }
before { question.answers << answer }
it { should == ['answer value'] }
end
context "when there are multiple answers present (note: for multi-answer questions, should implement logic in subclass)" do
let(:first_answer) { Answer.new(value: 'first answer value') }
let(:second_answer) { Answer.new(value: 'second answer value') }
before { question.answers << [first_answer, second_answer] }
it { should == ['first answer value'] }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment