Skip to content

Instantly share code, notes, and snippets.

@jimmycuadra
Created February 16, 2011 18:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmycuadra/c3234352b3c4776ce132 to your computer and use it in GitHub Desktop.
Save jimmycuadra/c3234352b3c4776ce132 to your computer and use it in GitHub Desktop.
# ...
config.active_record.observers = :comment_observer
# ...
class CommentObserver < ActiveRecord::Observer
def after_create(comment)
Notification.new_comment(comment).deliver
end
end
require 'spec_helper'
describe CommentObserver do
it "sends a notification mail after a new comment is created" do
@comment = Factory.build(:comment)
Notification.should_receive(:new_comment).with(@comment)
@comment.save
end
end
class Notification < ActionMailer::Base
def new_comment(comment)
@comment = comment
mail(:to => ENV["ADMIN_EMAIL"], :from => ENV["ADMIN_EMAIL"], :subject => "New comment from #{@comment.name} on #{@comment.post.title}")
end
end
require "spec_helper"
describe Notification do
describe "#new_comment" do
before(:each) do
ENV["ADMIN_EMAIL"] = 'admin@example.com'
@comment = Factory(:comment)
@email = Notification.new_comment(@comment).deliver
end
it "queues a message for new comments" do
ActionMailer::Base.deliveries.should_not be_empty
end
it "sends email to and from the admin" do
@email.to.should include(ENV["ADMIN_EMAIL"])
@email.from.should include(ENV["ADMIN_EMAIL"])
end
end
end
Notification
#new_comment
queues a message for new comments
sends email to and from the admin
CommentObserver
sends a notification mail after a new comment is created (FAILED - 1)
Failures:
1) CommentObserver sends a notification mail after a new comment is created
Failure/Error: @comment.save
NoMethodError:
undefined method `deliver' for nil:NilClass
# ./app/models/comment_observer.rb:3:in `after_create'
# ./spec/models/comment_observer_spec.rb:7:in `block (2 levels) in <top (required)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment