Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

topic controller tests

  • Loading branch information...
commit e2e39382df85573d5563a384235c3aa42a8d95b8 1 parent afa44f9
Eric Hutzelman authored
5 app/controllers/topics_controller.rb
View
@@ -21,7 +21,7 @@ def create
end
def destroy
- @topic = Topic.find(params[:id])
+ @topic = Topic.find_by_id_and_presenter_user_id(params[:id], current_user.id)
@topic.destroy
flash[:notice] = "Topic has been removed"
@@ -30,7 +30,8 @@ def destroy
def vote
@topic = Topic.find(params[:id])
- @topic.topic_votes.create(:user_id => current_user.id)
+ @topic_vote = TopicVote.find_by_topic_id_and_user_id(@topic.id, current_user.id)
+ @topic.topic_votes.create(:user_id => current_user.id) if !@topic_vote
flash[:notice] = "Your vote has been recorded"
redirect_to topics_path
2  config/initializers/backtrace_silencers.rb
View
@@ -6,5 +6,5 @@
# You can also remove all the silencers if you're trying do debug a problem that might steem from framework code.
# Rails.backtrace_cleaner.remove_silencers!
-Rails.backtrace_cleaner.add_silencer { |line| line =~ /thoughtbot/ }
+Rails.backtrace_cleaner.add_silencer { |line| line =~ /shoulda/ }
Rails.backtrace_cleaner.add_silencer { |line| line =~ /webrat/ }
11 test/factories.rb
View
@@ -6,14 +6,23 @@
"user#{n}"
end
+Factory.sequence :identity_url do |n|
+ "http://example#{n}.myopenid.com"
+end
+
Factory.define :user do |user|
user.name { Factory.next :name }
user.email { Factory.next :email }
- user.identity_url "http://example.myopenid.com"
+ user.identity_url { Factory.next :identity_url }
end
Factory.define :meeting do |meeting|
meeting.title "Houston Ruby Test Meeting"
meeting.start_at { 7.days.from_now }
meeting.end_at { |m| m.start_at + 2.hours }
+end
+
+Factory.define :topic do |topic|
+ topic.description "New Interesting Topic"
+ topic.requester { Factory :user }
end
87 test/functional/topics_controller_test.rb
View
@@ -1,8 +1,89 @@
require 'test_helper'
class TopicsControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+
+ context "get index" do
+ setup { get :index }
+
+ should_respond_with :success
+ should_assign_to :topics
+ end
+
+ context "create a new topic to present" do
+ setup do
+ login_as @user = Factory(:user)
+ post :create, :description => 'my new topic', :user_will_present => 'true'
+ end
+
+ should_change 'Topic.count', :by => 1
+ should_redirect_to("topics_path") { topics_path }
+ should_set_the_flash_to(/added/)
+
+ should "assign user as presenter" do
+ assert_equal @user, Topic.last.presenter
+ end
+
+ should "assign user as requester" do
+ assert_equal @user, Topic.last.requester
+ end
+ end
+
+ context "create a new topic to request" do
+ setup do
+ login_as @user = Factory(:user)
+ post :create, :description => 'my new topic', :user_will_present => 'false'
+ end
+
+ should_change('number of topics', :by => 1) { Topic.count }
+ should_redirect_to("topics_path") { topics_path }
+ should_set_the_flash_to(/added/)
+
+ should "assign user as requester" do
+ assert_equal @user, Topic.last.requester
+ end
+
+ should "not assign user as presenter" do
+ assert_not_equal @user, Topic.last.presenter
+ end
+ end
+
+ context "create vote for topic" do
+ setup do
+ login_as @user = Factory(:user)
+ @topic = Factory :topic
+ post :vote, :id => @topic.id
+ end
+
+ should_change('number of topic votes', :by => 1) { TopicVote.count }
+ should_redirect_to("topics_path") { topics_path }
+ should_set_the_flash_to(/recorded/)
+ end
+
+ context "remove a topic as presenter" do
+ setup do
+ login_as @user = Factory(:user)
+ @topic = Factory :topic, :presenter_user_id => @user.id
+ delete :destroy, :id => @topic.id
+ end
+
+ should_redirect_to("topics_path") { topics_path }
+ should_set_the_flash_to(/removed/)
+
+ should "delete the topic" do
+ assert_nil Topic.find_by_id(@topic.id)
+ end
+ end
+
+ context "volunteer to present a topic" do
+ setup do
+ login_as @user = Factory(:user)
+ @topic = Factory :topic
+ put :volunteer, :id => @topic.id
+ end
+
+ should "assign user as presenter" do
+ assert_equal @user, @topic.reload.presenter
+ end
+
end
end
4 test/test_helper.rb
View
@@ -37,6 +37,10 @@ class ActiveSupport::TestCase
# Add more helper methods to be used by all tests here...
end
+def login_as(user)
+ @request.session[:user_id] = user ? user.id : nil
+end
+
# mock retrieval of messages from yahoo groups
def mock_messages_xml
xml = File.open("#{RAILS_ROOT}/test/fixtures/messages.xml") do |f|
5 test/unit/topic_test.rb
View
@@ -1,8 +1,5 @@
require 'test_helper'
class TopicTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
+
end
5 test/unit/topic_vote_test.rb
View
@@ -1,8 +1,5 @@
require 'test_helper'
class TopicVoteTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.