choices_controller_spec.rb
4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe ChoicesController do
def sign_in_as(user)
@controller.current_user = user
return user
end
#
before(:each) do
sign_in_as(@user = Factory(:email_confirmed_user))
end
#
def mock_question(stubs={})
@mock_question ||= mock_model(Question, stubs)
end
def mock_prompt(stubs={})
@mock_prompt ||= mock_model(Prompt, stubs)
end
def mock_appearance(stubs={})
@mock_appearance||= mock_model(Appearance, stubs)
end
def mock_visitor(stubs={})
@mock_visitor||= mock_model(Visitor, stubs)
end
def mock_choice(stubs={})
@mock_choice||= mock_model(Choice, stubs)
end
def mock_flag(stubs={})
@mock_flag ||= mock_model(Flag, stubs)
end
def mock_vote(stubs={})
@mock_vote || mock_model(Vote, stubs)
end
describe "PUT flag" do
before(:each) do
question_list = [mock_question]
@user.stub!(:questions).and_return(question_list)
question_list.stub!(:find).with("37").and_return(mock_question)
choice_list = [mock_choice]
mock_question.stub!(:choices).and_return(choice_list)
choice_list.stub!(:find).with("123").and_return(mock_choice)
mock_choice.should_receive(:deactivate!).and_return(true)
end
it "deactives a choice when a flag request is sent" do
Flag.should_receive(:create!).with({:choice_id => 123, :question_id => 37, :site_id => @user.id})
put :flag, :id => 123, :question_id => 37
assigns[:choice].should == mock_choice
end
it "adds explanation params to flag if sent" do
Flag.should_receive(:create!).with({:choice_id => 123, :question_id => 37, :site_id => @user.id, :explanation => "This is offensive"})
put :flag, :id => 123, :question_id => 37 , :explanation => "This is offensive"
assigns[:choice].should == mock_choice
end
it "adds visitor_id params to flag if sent" do
@visitor_identifier = "somelongunique32charstring"
visitor_list = [mock_visitor]
@user.stub!(:visitors).and_return(visitor_list)
visitor_list.should_receive(:find_or_create_by_identifier).with(@visitor_identifier).and_return(mock_visitor)
Flag.should_receive(:create!).with({:choice_id => 123, :question_id => 37, :site_id => @user.id, :explanation => "This is offensive", :visitor_id => mock_visitor.id})
put :flag, :id => 123, :question_id => 37 , :explanation => "This is offensive", :visitor_identifier => @visitor_identifier
assigns[:choice].should == mock_choice
end
end
describe "POST create" do
before(:each) do
@question = Factory.create(:aoi_question)
sign_in_as(@user = @question.site)
end
it "creates a choice" do
post :create, :question_id => @question.id, :choice => {:data => "blahblah"}
assigns[:choice].should_not be_nil
assigns[:choice].creator.should == @question.site.default_visitor
assigns[:choice].should_not be_active
assigns[:choice].should_not be_active
end
it "creates a choice with a correct visitor creator" do
post :create, :question_id => @question.id, :choice => {:data => "blahblah", :visitor_identifier => "new user"}
assigns[:choice].should_not be_nil
assigns[:choice].creator.identifier.should == "new user"
assigns[:choice].should_not be_active
assigns[:choice].user_created.should == true
end
it "creates a choice and activates it when set_autoactivate_is set" do
@question.update_attribute(:it_should_autoactivate_ideas, true)
post :create, :question_id => @question.id, :choice => {:data => "blahblah"}
assigns[:choice].should_not be_nil
assigns[:choice].creator.should == @question.site.default_visitor
assigns[:choice].should be_active
end
end
describe "GET votes" do
it "returns a choice's votes" do
Choice.should_receive(:find).and_return(mock_choice)
votes_array = [mock_vote]
votes_array.should_receive(:to_xml)
mock_choice.should_receive(:votes).and_return(votes_array)
get :votes, :id => mock_choice.id, :question_id => mock_question.id
end
end
end