Commit 578ad0c74e31c77add5ddc3c190a3f2cd2c99dcd
1 parent
439818b8
Exists in
master
and in
1 other branch
Created aoi_question factory to simplify spec setup code
Showing
5 changed files
with
36 additions
and
32 deletions
Show diff stats
app/models/question.rb
... | ... | @@ -24,7 +24,7 @@ class Question < ActiveRecord::Base |
24 | 24 | attr_accessor :ideas |
25 | 25 | after_create :create_choices_from_ideas |
26 | 26 | def create_choices_from_ideas |
27 | - if ideas.any? | |
27 | + if ideas && ideas.any? | |
28 | 28 | ideas.each do |idea| |
29 | 29 | item = Item.create!(:data => idea.squish.strip, :creator => self.creator) |
30 | 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 | 2 | |
3 | 3 | describe QuestionsController do |
4 | 4 | |
5 | - # integrate_views | |
6 | - # | |
7 | 5 | def sign_in_as(user) |
8 | 6 | @controller.current_user = user |
9 | 7 | return user |
10 | 8 | end |
11 | - # | |
9 | + | |
12 | 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 | 14 | end |
17 | 15 | it "responds with basic question information" do |
18 | 16 | get :show, :id => @question.id, :format => "xml" |
19 | 17 | |
20 | 18 | assigns[:question].should == @question |
21 | 19 | @response.body.should have_tag("question") |
20 | + @response.code.should == "200" | |
22 | 21 | end |
23 | 22 | |
24 | 23 | |
... | ... | @@ -27,6 +26,7 @@ describe QuestionsController do |
27 | 26 | |
28 | 27 | assigns[:question].should == @question |
29 | 28 | #@response.body.should be_nil |
29 | + @response.code.should == "200" | |
30 | 30 | @response.body.should have_tag("question") |
31 | 31 | @response.body.should have_tag("picked_prompt_id") |
32 | 32 | @response.body.should have_tag("appearance_id") | ... | ... |
spec/factories.rb
... | ... | @@ -2,17 +2,31 @@ Factory.define(:item) do |f| |
2 | 2 | f.sequence(:data) { |i| "Item #{i}" } |
3 | 3 | end |
4 | 4 | |
5 | + | |
5 | 6 | Factory.define(:question) do |f| |
6 | 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 | 10 | end |
8 | 11 | Factory.define(:aoi_question, :parent => :question) do |f| |
9 | 12 | f.sequence(:name) { |i| "Name #{i}" } |
10 | 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 | 25 | end |
13 | 26 | |
14 | 27 | Factory.define(:visitor) do |f| |
15 | 28 | f.sequence(:identifier) { |i| "Identifier #{i}" } |
29 | + f.association :site, :factory => :user | |
16 | 30 | end |
17 | 31 | |
18 | 32 | Factory.define(:prompt) do |f| |
... | ... | @@ -21,6 +35,8 @@ end |
21 | 35 | |
22 | 36 | Factory.define(:choice) do |f| |
23 | 37 | f.sequence(:data) { |i| "Choice: #{i}" } |
38 | + f.association :question | |
39 | + f.creator {|c| c.association(:visitor, :site => c.question.site)} | |
24 | 40 | end |
25 | 41 | |
26 | 42 | Factory.sequence :email do |n| | ... | ... |
spec/models/question_spec.rb
... | ... | @@ -13,24 +13,10 @@ describe Question do |
13 | 13 | it {should validate_presence_of :creator} |
14 | 14 | |
15 | 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 | 20 | end |
35 | 21 | |
36 | 22 | it "should have 2 active choices" do |
... | ... | @@ -38,7 +24,8 @@ describe Question do |
38 | 24 | end |
39 | 25 | |
40 | 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 | 29 | end |
43 | 30 | |
44 | 31 | it "should not create two default choices if none are provided" do |
... | ... | @@ -116,15 +103,15 @@ describe Question do |
116 | 103 | |
117 | 104 | context "catchup algorithm" do |
118 | 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 | 108 | @catchup_q.it_should_autoactivate_ideas = true |
123 | 109 | @catchup_q.uses_catchup = true |
124 | 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 | 115 | end |
129 | 116 | @catchup_q.reload |
130 | 117 | end |
... | ... | @@ -202,8 +189,9 @@ describe Question do |
202 | 189 | |
203 | 190 | context "exporting data" do |
204 | 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 | 195 | @question.it_should_autoactivate_ideas = true |
208 | 196 | @question.save! |
209 | 197 | ... | ... |
test/factories.rb