Commit 578ad0c74e31c77add5ddc3c190a3f2cd2c99dcd

Authored by Dhruv Kapadia
1 parent 439818b8

Created aoi_question factory to simplify spec setup code

app/models/question.rb
@@ -24,7 +24,7 @@ class Question < ActiveRecord::Base @@ -24,7 +24,7 @@ class Question < ActiveRecord::Base
24 attr_accessor :ideas 24 attr_accessor :ideas
25 after_create :create_choices_from_ideas 25 after_create :create_choices_from_ideas
26 def create_choices_from_ideas 26 def create_choices_from_ideas
27 - if ideas.any? 27 + if ideas && ideas.any?
28 ideas.each do |idea| 28 ideas.each do |idea|
29 item = Item.create!(:data => idea.squish.strip, :creator => self.creator) 29 item = Item.create!(:data => idea.squish.strip, :creator => self.creator)
30 choices.create!(:item => item, :creator => self.creator, :active => true, :data => idea.squish.strip) 30 choices.create!(:item => item, :creator => self.creator, :active => true, :data => idea.squish.strip)
spec/controllers/questions_controller_spec.rb
@@ -2,23 +2,22 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') @@ -2,23 +2,22 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2 2
3 describe QuestionsController do 3 describe QuestionsController do
4 4
5 - # integrate_views  
6 - #  
7 def sign_in_as(user) 5 def sign_in_as(user)
8 @controller.current_user = user 6 @controller.current_user = user
9 return user 7 return user
10 end 8 end
11 - # 9 +
12 before(:each) do 10 before(:each) do
13 - @user = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8)  
14 - sign_in_as(@user = Factory(:email_confirmed_user))  
15 - @question = @user.create_question("foobarbaz", {:name => 'foo'}) 11 + @question = Factory.create(:aoi_question)
  12 + sign_in_as(@user = @question.site)
  13 + @creator = @question.creator
16 end 14 end
17 it "responds with basic question information" do 15 it "responds with basic question information" do
18 get :show, :id => @question.id, :format => "xml" 16 get :show, :id => @question.id, :format => "xml"
19 17
20 assigns[:question].should == @question 18 assigns[:question].should == @question
21 @response.body.should have_tag("question") 19 @response.body.should have_tag("question")
  20 + @response.code.should == "200"
22 end 21 end
23 22
24 23
@@ -27,6 +26,7 @@ describe QuestionsController do @@ -27,6 +26,7 @@ describe QuestionsController do
27 26
28 assigns[:question].should == @question 27 assigns[:question].should == @question
29 #@response.body.should be_nil 28 #@response.body.should be_nil
  29 + @response.code.should == "200"
30 @response.body.should have_tag("question") 30 @response.body.should have_tag("question")
31 @response.body.should have_tag("picked_prompt_id") 31 @response.body.should have_tag("picked_prompt_id")
32 @response.body.should have_tag("appearance_id") 32 @response.body.should have_tag("appearance_id")
spec/factories.rb
@@ -2,17 +2,31 @@ Factory.define(:item) do |f| @@ -2,17 +2,31 @@ Factory.define(:item) do |f|
2 f.sequence(:data) { |i| "Item #{i}" } 2 f.sequence(:data) { |i| "Item #{i}" }
3 end 3 end
4 4
  5 +
5 Factory.define(:question) do |f| 6 Factory.define(:question) do |f|
6 f.sequence(:name) { |i| "Name #{i}" } 7 f.sequence(:name) { |i| "Name #{i}" }
  8 + f.site {|s| s.association(:user)}
  9 + f.creator {|c| c.association(:visitor, :site => c.site)}
7 end 10 end
8 Factory.define(:aoi_question, :parent => :question) do |f| 11 Factory.define(:aoi_question, :parent => :question) do |f|
9 f.sequence(:name) { |i| "Name #{i}" } 12 f.sequence(:name) { |i| "Name #{i}" }
10 f.association :site, :factory => :user 13 f.association :site, :factory => :user
11 - f.association :creator, :factory => :visitor 14 + f.creator {|c| c.association(:visitor, :site => c.site)}
  15 + f.choices do |question|
  16 + result = []
  17 + 2.times do
  18 + result << Factory.build(:choice,
  19 + :question => question.result,
  20 + :creator => question.creator,
  21 + :active => true)
  22 + end
  23 + result
  24 + end
12 end 25 end
13 26
14 Factory.define(:visitor) do |f| 27 Factory.define(:visitor) do |f|
15 f.sequence(:identifier) { |i| "Identifier #{i}" } 28 f.sequence(:identifier) { |i| "Identifier #{i}" }
  29 + f.association :site, :factory => :user
16 end 30 end
17 31
18 Factory.define(:prompt) do |f| 32 Factory.define(:prompt) do |f|
@@ -21,6 +35,8 @@ end @@ -21,6 +35,8 @@ end
21 35
22 Factory.define(:choice) do |f| 36 Factory.define(:choice) do |f|
23 f.sequence(:data) { |i| "Choice: #{i}" } 37 f.sequence(:data) { |i| "Choice: #{i}" }
  38 + f.association :question
  39 + f.creator {|c| c.association(:visitor, :site => c.question.site)}
24 end 40 end
25 41
26 Factory.sequence :email do |n| 42 Factory.sequence :email do |n|
spec/models/question_spec.rb
@@ -13,24 +13,10 @@ describe Question do @@ -13,24 +13,10 @@ describe Question do
13 it {should validate_presence_of :creator} 13 it {should validate_presence_of :creator}
14 14
15 before(:each) do 15 before(:each) do
16 - @aoi_clone = Factory.create(:user,:password => "password", :password_confirmation => "password", :id => 8)  
17 - @valid_attributes = {  
18 - :site => @aoi_clone,  
19 - :creator => @aoi_clone.default_visitor  
20 -  
21 - } 16 +
  17 + @question = Factory.create(:aoi_question)
  18 + @aoi_clone = @question.site
22 19
23 - @question = @aoi_clone.create_question("foobarbaz", {:name => 'foo'})  
24 - @question.it_should_autoactivate_ideas = true  
25 - @question.save!  
26 -  
27 - 2.times.each do |num|  
28 - @aoi_clone.create_choice("visitor identifier", @question, {:data => num.to_s, :local_identifier => "example"})  
29 - end  
30 -  
31 - @question.reload  
32 -  
33 -  
34 end 20 end
35 21
36 it "should have 2 active choices" do 22 it "should have 2 active choices" do
@@ -38,7 +24,8 @@ describe Question do @@ -38,7 +24,8 @@ describe Question do
38 end 24 end
39 25
40 it "should create a new instance given valid attributes" do 26 it "should create a new instance given valid attributes" do
41 - Question.create!(@valid_attributes) 27 + # Factory.attributes_for does not return associations, this is a good enough substitute
  28 + Question.create!(Factory.build(:question).attributes.symbolize_keys)
42 end 29 end
43 30
44 it "should not create two default choices if none are provided" do 31 it "should not create two default choices if none are provided" do
@@ -116,15 +103,15 @@ describe Question do @@ -116,15 +103,15 @@ describe Question do
116 103
117 context "catchup algorithm" do 104 context "catchup algorithm" do
118 before(:all) do 105 before(:all) do
119 - user = Factory.create(:user)  
120 - @catchup_q = Factory.create(:aoi_question, :site => user, :creator => user.default_visitor) 106 + @catchup_q = Factory.create(:aoi_question)
121 107
122 @catchup_q.it_should_autoactivate_ideas = true 108 @catchup_q.it_should_autoactivate_ideas = true
123 @catchup_q.uses_catchup = true 109 @catchup_q.uses_catchup = true
124 @catchup_q.save! 110 @catchup_q.save!
125 111
126 - 100.times.each do |num|  
127 - user.create_choice("visitor identifier", @catchup_q, {:data => num.to_s, :local_identifier => "exmaple"}) 112 + # 2 ideas already exist, so this will make an even hundred
  113 + 98.times.each do |num|
  114 + @catchup_q.site.create_choice("visitor identifier", @catchup_q, {:data => num.to_s, :local_identifier => "exmaple"})
128 end 115 end
129 @catchup_q.reload 116 @catchup_q.reload
130 end 117 end
@@ -202,8 +189,9 @@ describe Question do @@ -202,8 +189,9 @@ describe Question do
202 189
203 context "exporting data" do 190 context "exporting data" do
204 before(:all) do 191 before(:all) do
205 - user = Factory.create(:user)  
206 - @question = Factory.create(:aoi_question, :site => user, :creator => user.default_visitor) 192 + @question = Factory.create(:aoi_question)
  193 + user = @question.site
  194 +
207 @question.it_should_autoactivate_ideas = true 195 @question.it_should_autoactivate_ideas = true
208 @question.save! 196 @question.save!
209 197
test/factories.rb