* Updated gemfile

* Added forum post/topic unit tests
* Added forum post/topic controller tests
This commit is contained in:
albert
2011-01-12 18:00:07 -05:00
parent 46164eab4f
commit 668fbab77a
18 changed files with 342 additions and 105 deletions

View File

@@ -1,4 +1,3 @@
Factory.define(:forum_post) do |f|
f.creator {|x| x.association(:user)}
f.body {Faker::Lorem.sentences}
end

View File

@@ -1,5 +1,4 @@
Factory.define(:forum_topic) do |f|
f.creator {|x| x.association(:user)}
f.title {Faker::Lorem.words}
f.is_sticky false
f.is_locked false

View File

@@ -1,8 +1,80 @@
require 'test_helper'
class ForumTopicsControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
context "The forum topics controller" do
setup do
@user = Factory.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@other_user = Factory.create(:user)
@mod = Factory.create(:moderator_user)
@forum_topic = Factory.create(:forum_topic, :title => "my forum topic", :creator => @user)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "index action" do
should "list all forum topics" do
get :index
assert_response :success
end
context "with search conditions" do
should "list all matching forum topics" do
get :index, {:search => {:title_matches => "forum"}}
assert_response :success
assert_equal(1, assigns(:forum_topics).size)
end
should "list nothing for when the search matches nothing" do
get :index, {:search => {:title_matches => "bababa"}}
assert_response :success
assert_equal(0, assigns(:forum_topics).size)
end
end
end
context "edit action" do
should "render if the editor is the creator of the topic" do
get :edit, {:id => @forum_topic.id}, {:user_id => @user.id}
assert_response :success
end
should "render if the editor is a moderator" do
get :edit, {:id => @forum_topic.id}, {:user_id => @mod.id}
assert_response :success
end
should "fail if the editor is not the creator of the topic and is not a moderator" do
assert_raises(User::PrivilegeError) do
get :edit, {:id => @forum_topic.id}, {:user_id => @other_user.id}
end
end
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
assert_response :success
end
end
context "create action" do
should "create a new forum topic and post" do
assert_difference(["ForumPost.count", "ForumTopic.count"], 1) do
post :create, {:forum_topic => {:title => "bababa", :original_post_attributes => {:body => "xaxaxa"}}}, {:user_id => @user.id}
end
forum_topic = ForumTopic.last
assert_redirected_to(forum_topic_path(forum_topic))
end
end
context "destroy action" do
end
end
end

View File

@@ -18,7 +18,7 @@ class DmailTest < ActiveSupport::TestCase
context "search" do
should "return results based on title contents" do
dmail = Factory.create(:dmail, :title => "xxx")
dmail = Factory.create(:dmail, :title => "xxx", :owner => @user)
matches = Dmail.search_message("xxx")
assert(matches.any?)
matches = Dmail.search_message("aaa")
@@ -26,7 +26,7 @@ class DmailTest < ActiveSupport::TestCase
end
should "return results based on body contents" do
dmail = Factory.create(:dmail, :body => "xxx")
dmail = Factory.create(:dmail, :body => "xxx", :owner => @user)
matches = Dmail.search_message("xxx")
assert(matches.any?)
matches = Dmail.search_message("aaa")
@@ -35,14 +35,14 @@ class DmailTest < ActiveSupport::TestCase
end
should "should parse user names" do
dmail = Factory.build(:dmail)
dmail = Factory.build(:dmail, :owner => @user)
dmail.to_id = nil
dmail.to_name = @user.name
assert(dmail.to_id == @user.id)
end
should "construct a response" do
dmail = Factory.create(:dmail)
dmail = Factory.create(:dmail, :owner => @user)
response = dmail.build_response
assert_equal("Re: #{dmail.title}", response.title)
assert_equal(dmail.from_id, response.to_id)
@@ -58,19 +58,19 @@ class DmailTest < ActiveSupport::TestCase
should "send an email if the user wants it" do
user = Factory.create(:user, :receive_email_notifications => true)
assert_difference("ActionMailer::Base.deliveries.size", 1) do
Factory.create(:dmail, :to => user)
Factory.create(:dmail, :to => user, :owner => @user)
end
end
should "be marked as read after the user reads it" do
dmail = Factory.create(:dmail)
dmail = Factory.create(:dmail, :owner => @user)
assert(!dmail.is_read?)
dmail.mark_as_read!
assert(dmail.is_read?)
end
should "notify the recipient he has mail" do
dmail = Factory.create(:dmail)
dmail = Factory.create(:dmail, :owner => @user)
assert(dmail.to(true).has_mail?)
dmail.mark_as_read!
assert(!dmail.to(true).has_mail?)

View File

@@ -2,12 +2,47 @@ require_relative '../test_helper'
class ForumPostTest < ActiveSupport::TestCase
context "A forum post" do
setup do
@user = Factory.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@topic = Factory.create(:forum_topic)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "update its parent when saved" do
topic = Factory.create(:forum_topic)
sleep 2
post = Factory.create(:forum_post, :topic_id => topic.id)
topic.reload
assert(topic.updated_at > 1.second.ago)
sleep 1
post = Factory.create(:forum_post, :topic_id => @topic.id)
@topic.reload
assert(@topic.updated_at > 1.second.ago)
end
should "be searchable by body content" do
post = Factory.create(:forum_post, :topic_id => @topic.id, :body => "xxx")
assert_equal(1, ForumPost.body_matches("xxx").count)
assert_equal(0, ForumPost.body_matches("aaa").count)
end
should "initialize its creator" do
post = Factory.create(:forum_post, :topic_id => @topic.id)
assert_equal(@user.id, post.creator_id)
end
context "updated by a second user" do
setup do
@post = Factory.create(:forum_post, :topic_id => @topic.id)
@second_user = Factory.create(:user)
CurrentUser.user = @second_user
end
should "record its updater" do
@post.update_attributes(:body => "abc")
assert_equal(@second_user.id, @post.updater_id)
end
end
end
end

View File

@@ -1,4 +1,46 @@
require_relative '../test_helper'
class ForumTopicTest < ActiveSupport::TestCase
context "A forum topic" do
setup do
@user = Factory.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@topic = Factory.create(:forum_topic, :title => "xxx")
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "constructed with nested attributes for its original post" do
should "create a matching forum post" do
assert_difference(["ForumTopic.count", "ForumPost.count"], 1) do
@topic = Factory.create(:forum_topic, :title => "abc", :original_post_attributes => {:body => "abc"})
end
end
end
should "be searchable by title" do
assert_equal(1, ForumTopic.title_matches("xxx").count)
assert_equal(0, ForumTopic.title_matches("aaa").count)
end
should "initialize its creator" do
assert_equal(@user.id, @topic.creator_id)
end
context "updated by a second user" do
setup do
@second_user = Factory.create(:user)
CurrentUser.user = @second_user
end
should "record its updater" do
@topic.update_attributes(:title => "abc")
assert_equal(@second_user.id, @topic.updater_id)
end
end
end
end