使用rspec测试after_create挂钩

ngw*_*ngw 5 rspec rspec2 rspec-rails ruby-on-rails-3

我的模型(RoR 3.0.x)中的代码或多或少是这样的:

class Message

    after_create :notify

    protected

    def notify
        if visible?
            Notifier.message_from_portfolio( user, self ).deliver
        else
            Notifier.invisible_message_from_portfolio( user, self ).deliver
        end
    end

end
Run Code Online (Sandbox Code Playgroud)

我正在使用最新的rspec gem来测试它.问题是我无法测试通知方法:如果我直接测试它我不能因为它受到保护,如果我创建一条消息并设置期望它不起作用,因为很明显即使rspec运行通知metod我无法及时赶上电话.

我的规格是:

describe :notification do
    it "should send the whole message by email when visible" do
        u = Factory.create( :user, :account_type => 1 )
        message = u.messages.build( :body => "Whatever", :author => "Nobody", :email => "test@example.com" )
        Notifier.should_receive( :message_from_portfolio )
        message.save
    end
end
Run Code Online (Sandbox Code Playgroud)

对象通知程序永远不会收到message_from_portfolio.我究竟做错了什么?建议?

Ser*_*abe 3

Factory.create已保存消息,因此并未创建它,只是保存了。替换它Factory.build,一切都应该没问题。