Commit 468141a702c1d9408aab24dbd575e92476629130

Authored by Dhruv Kapadia
2 parents 629c4b02 a36f7203

Merge branch 'fixSpecBug'

spec/controllers/prompts_controller_spec.rb
@@ -7,13 +7,9 @@ describe PromptsController do @@ -7,13 +7,9 @@ describe PromptsController do
7 end 7 end
8 # 8 #
9 before(:each) do 9 before(:each) do
10 - @aoi_clone = Factory.create(:user)  
11 - sign_in_as(@user = Factory(:email_confirmed_user))  
12 - @johndoe = Factory.create(:visitor, :identifier => 'johndoe', :site => @aoi_clone)  
13 - @question = Factory.create(:question, :name => 'which do you like better?', :site => @aoi_clone, :creator => @aoi_clone.default_visitor)  
14 - @lc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'hello gorgeous')  
15 - @rc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'goodbye gorgeous')  
16 - @prompt = Factory.create(:prompt, :question => @question, :tracking => 'sample', :left_choice => @lc, :right_choice => @rc) 10 + @question = Factory.create(:aoi_question)
  11 + sign_in_as(@aoi_clone = @question.site)
  12 + @prompt = @question.prompts.first
17 13
18 @visitor = @aoi_clone.visitors.find_or_create_by_identifier("test_visitor_identifier") 14 @visitor = @aoi_clone.visitors.find_or_create_by_identifier("test_visitor_identifier")
19 @appearance = @aoi_clone.record_appearance(@visitor, @prompt) 15 @appearance = @aoi_clone.record_appearance(@visitor, @prompt)
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/choice_spec.rb
@@ -8,12 +8,14 @@ describe Choice do @@ -8,12 +8,14 @@ describe Choice do
8 it {should validate_presence_of :question} 8 it {should validate_presence_of :question}
9 9
10 before(:each) do 10 before(:each) do
11 - @aoi_clone = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8)  
12 - @johndoe = Factory.create(:visitor, :identifier => 'johndoe', :site => @aoi_clone)  
13 - @question = Question.create(:name => 'which do you like better?', :site => @aoi_clone, :creator => @johndoe) 11 + @aoi_clone = Factory.create(:email_confirmed_user)
  12 + @visitor= Factory.create(:visitor, :site => @aoi_clone)
  13 + @question = Question.create(:name => 'which do you like better?',
  14 + :site => @aoi_clone,
  15 + :creator => @visitor)
14 16
15 @valid_attributes = { 17 @valid_attributes = {
16 - :creator => @johndoe, 18 + :creator => @visitor,
17 :question => @question, 19 :question => @question,
18 :data => 'hi there' 20 :data => 'hi there'
19 } 21 }
@@ -23,11 +25,11 @@ describe Choice do @@ -23,11 +25,11 @@ describe Choice do
23 Choice.create!(@valid_attributes) 25 Choice.create!(@valid_attributes)
24 end 26 end
25 27
26 - it "should generate prompts after creation" do  
27 - prev_choices = @question.choices.size  
28 - @question.prompts.should_not be_empty  
29 - proc {choice1 = Choice.create!(@valid_attributes.merge(:data => '1234'))}.should change(@question.prompts, :count).by(prev_choices*2)  
30 - @question.prompts.should_not be_empty 28 + it "should generate prompts after two choices are created" do
  29 + proc {
  30 + choice1 = Choice.create!(@valid_attributes.merge(:data => '1234'))
  31 + choice2 = Choice.create!(@valid_attributes.merge(:data => '1234'))
  32 + }.should change(@question.prompts, :count).by(2)
31 end 33 end
32 34
33 it "should deactivate a choice" do 35 it "should deactivate a choice" do
@@ -37,10 +39,13 @@ describe Choice do @@ -37,10 +39,13 @@ describe Choice do
37 end 39 end
38 40
39 it "should update a question's counter cache on creation" do 41 it "should update a question's counter cache on creation" do
40 - @question.choices.size.should == 2  
41 - choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) 42 + @question.choices_count.should == 0
  43 + @question.choices.size.should == 0
  44 + Choice.create!(@valid_attributes.merge(:data => '1234'))
42 @question.reload 45 @question.reload
43 - @question.choices.size.should == 3 46 + @question.choices_count.should == 1
  47 + @question.choices.size.should == 1
  48 +
44 end 49 end
45 50
46 it "should update a question's counter cache on activation" do 51 it "should update a question's counter cache on activation" do
spec/models/prompt_spec.rb
@@ -5,22 +5,4 @@ describe Prompt do @@ -5,22 +5,4 @@ describe Prompt do
5 it {should belong_to :left_choice} 5 it {should belong_to :left_choice}
6 it {should belong_to :right_choice} 6 it {should belong_to :right_choice}
7 7
8 - before(:each) do  
9 - @aoi_clone = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8)  
10 - @johndoe = Factory.create(:visitor, :identifier => 'johndoe', :site => @aoi_clone)  
11 - @question = Factory.create(:question, :name => 'which do you like better?', :site => @aoi_clone, :creator => @aoi_clone.default_visitor)  
12 - @lc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'hello gorgeous')  
13 - @rc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'goodbye gorgeous')  
14 - @prompt = Factory.create(:prompt, :question => @question, :tracking => 'sample', :left_choice => @lc, :right_choice => @rc)  
15 - @valid_attributes = {  
16 - :left_choice => @lc,  
17 - :right_choice => @rc,  
18 - :question => @question  
19 -  
20 - }  
21 - end  
22 -  
23 - it "should create a new instance given valid attributes" do  
24 -  
25 - end  
26 end 8 end
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
spec/models/user_spec.rb
@@ -5,14 +5,9 @@ describe User do @@ -5,14 +5,9 @@ describe User do
5 5
6 before(:each) do 6 before(:each) do
7 @aoi_clone = Factory.create(:user) 7 @aoi_clone = Factory.create(:user)
8 - @johndoe = Factory.create(:visitor, :identifier => 'johndoe', :site => @aoi_clone)  
9 - @question = Factory.create(:question, :name => 'which do you like better?', :site => @aoi_clone, :creator => @aoi_clone.default_visitor)  
10 - @lc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'hello gorgeous')  
11 - @rc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'goodbye gorgeous')  
12 - @prompt = Factory.create(:prompt, :question => @question, :tracking => 'sample', :left_choice => @lc, :right_choice => @rc) 8 + @question = Factory.create(:aoi_question)
  9 + @prompt = @question.prompts.first
13 10
14 - @visitor = @aoi_clone.visitors.find_or_create_by_identifier("test_visitor_identifier")  
15 - @appearance = @aoi_clone.record_appearance(@visitor, @prompt)  
16 end 11 end
17 12
18 13
@@ -28,10 +23,12 @@ describe User do @@ -28,10 +23,12 @@ describe User do
28 c = @aoi_clone.create_choice("foobarbaz", q, {:data => 'foobarbaz'}) 23 c = @aoi_clone.create_choice("foobarbaz", q, {:data => 'foobarbaz'})
29 q.should_not be_nil 24 q.should_not be_nil
30 q.choices.should_not be_empty 25 q.choices.should_not be_empty
31 - q.choices.size.should eql 3 26 + q.choices.size.should eql 1
32 end 27 end
33 28
34 it "should be able to record a visitor's vote" do 29 it "should be able to record a visitor's vote" do
  30 + @visitor = @aoi_clone.visitors.find_or_create_by_identifier("test_visitor_identifier")
  31 + @appearance = @aoi_clone.record_appearance(@visitor, @prompt)
35 v = @aoi_clone.record_vote("johnnydoe", @appearance.lookup, @prompt, 0, 304) 32 v = @aoi_clone.record_vote("johnnydoe", @appearance.lookup, @prompt, 0, 304)
36 prompt_votes = @prompt.votes(true) 33 prompt_votes = @prompt.votes(true)
37 prompt_votes.should_not be_empty 34 prompt_votes.should_not be_empty
@@ -47,6 +44,8 @@ describe User do @@ -47,6 +44,8 @@ describe User do
47 end 44 end
48 45
49 it "should be able to record a visitor's skip" do 46 it "should be able to record a visitor's skip" do
  47 + @visitor = @aoi_clone.visitors.find_or_create_by_identifier("test_visitor_identifier")
  48 + @appearance = @aoi_clone.record_appearance(@visitor, @prompt)
50 s = @aoi_clone.record_skip("johnnydoe", @appearance.lookup, @prompt, 340) 49 s = @aoi_clone.record_skip("johnnydoe", @appearance.lookup, @prompt, 340)
51 end 50 end
52 51
spec/models/visitor_spec.rb
@@ -9,12 +9,13 @@ describe Visitor do @@ -9,12 +9,13 @@ describe Visitor do
9 it {should have_many :clicks} 9 it {should have_many :clicks}
10 10
11 before(:each) do 11 before(:each) do
12 - @aoi_clone = Factory.create(:user)  
13 - @johndoe = Factory.create(:visitor, :identifier => 'johndoe', :site => @aoi_clone)  
14 - @question = Factory.create(:question, :name => 'which do you like better?', :site => @aoi_clone, :creator => @aoi_clone.default_visitor)  
15 - @lc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'hello gorgeous')  
16 - @rc = Factory.create(:choice, :question => @question, :creator => @johndoe, :data => 'goodbye gorgeous')  
17 - @prompt = Factory.create(:prompt, :question => @question, :tracking => 'sample', :left_choice => @lc, :right_choice => @rc) 12 + @question = Factory.create(:aoi_question)
  13 + @aoi_clone = @question.site
  14 + @johndoe = @question.creator
  15 +
  16 + @prompt = @question.prompts.first
  17 + @lc = @prompt.left_choice
  18 + @rc = @prompt.right_choice
18 19
19 @visitor = @aoi_clone.visitors.find_or_create_by_identifier("test_visitor_identifier") 20 @visitor = @aoi_clone.visitors.find_or_create_by_identifier("test_visitor_identifier")
20 @appearance = @aoi_clone.record_appearance(@visitor, @prompt) 21 @appearance = @aoi_clone.record_appearance(@visitor, @prompt)
@@ -23,16 +24,15 @@ describe Visitor do @@ -23,16 +24,15 @@ describe Visitor do
23 :identifier => "value for identifier", 24 :identifier => "value for identifier",
24 :tracking => "value for tracking" 25 :tracking => "value for tracking"
25 } 26 }
26 - @v = Visitor.create!(@valid_attributes)  
27 -  
28 end 27 end
29 28
30 it "should create a new instance given valid attributes" do 29 it "should create a new instance given valid attributes" do
  30 + @visitor = Visitor.create!(@valid_attributes)
31 31
32 end 32 end
33 33
34 it "should be able to determine ownership of a question" do 34 it "should be able to determine ownership of a question" do
35 - @v.owns?(Question.new).should be_false 35 + @visitor.owns?(Question.new).should be_false
36 36
37 ownedquestion = Factory.create(:question, :site => @aoi_clone, :creator=> @johndoe) 37 ownedquestion = Factory.create(:question, :site => @aoi_clone, :creator=> @johndoe)
38 @johndoe.owns?(ownedquestion).should be_true 38 @johndoe.owns?(ownedquestion).should be_true
@@ -41,20 +41,20 @@ describe Visitor do @@ -41,20 +41,20 @@ describe Visitor do
41 it "should be able to vote for a prompt" do 41 it "should be able to vote for a prompt" do
42 #@prompt = @question.prompts.first 42 #@prompt = @question.prompts.first
43 @prompt.should_not be_nil 43 @prompt.should_not be_nil
44 - v = @v.vote_for! @appearance.lookup, @prompt, 0, 340 44 + v = @visitor.vote_for! @appearance.lookup, @prompt, 0, 340
45 end 45 end
46 46
47 it "should be able to skip a prompt" do 47 it "should be able to skip a prompt" do
48 #@prompt = @question.prompts.first 48 #@prompt = @question.prompts.first
49 @prompt.should_not be_nil 49 @prompt.should_not be_nil
50 - v = @v.skip! @appearance.lookup, @prompt, 304 50 + v = @visitor.skip! @appearance.lookup, @prompt, 304
51 end 51 end
52 52
53 it "should accurately update score counts after vote" do 53 it "should accurately update score counts after vote" do
54 prev_winner_score = @lc.score 54 prev_winner_score = @lc.score
55 prev_loser_score = @rc.score 55 prev_loser_score = @rc.score
56 56
57 - vote = @v.vote_for! @appearance.lookup, @prompt, 0, 340 57 + vote = @visitor.vote_for! @appearance.lookup, @prompt, 0, 340
58 58
59 @lc.reload 59 @lc.reload
60 @rc.reload 60 @rc.reload
@@ -69,7 +69,7 @@ describe Visitor do @@ -69,7 +69,7 @@ describe Visitor do
69 prev_loser_losses = @rc.losses 69 prev_loser_losses = @rc.losses
70 prev_loser_wins = @rc.wins 70 prev_loser_wins = @rc.wins
71 71
72 - vote = @v.vote_for! @appearance.lookup, @prompt, 0, 340 72 + vote = @visitor.vote_for! @appearance.lookup, @prompt, 0, 340
73 73
74 @lc.reload 74 @lc.reload
75 @rc.reload 75 @rc.reload
test/factories.rb