论坛首页 Ruby版 rails

Rspec测试的问题?

浏览 155 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-05-09
如何测试mail , observer啊?自己建立一个?
有没有generate可以直接生成呢?好像有可以生成建立plugin的rspec的generate……
   
时间:2008-05-09
自己解决
require File.dirname(__FILE__) + '/../spec_helper'

context "A UserNotifier on signup_notification" do
setup do
set_mailer_in_test

@user = mock("user")
@user.stub!(:email).and_return('user@test.com')
@user.stub!(:activation_code).and_return('code')
@user.stub!(:login).and_return('user')
@user.stub!(:password).and_return('password')

@user_notifier = UserNotifier.create_signup_notification(@user)
end

specify "should set @recipients to user's email" do
@user_notifier.to.should_eql ["user@test.com"]
end

specify "should set @subject to [YOURSITE] Please activate your new account" do
@user_notifier.subject.should_eql "[YOURSITE] Please activate your new account"
end

specify "should set @from to ADMINEMAIL" do
@user_notifier.from.should_eql ["ADMINEMAIL"]
end

specify "should contain login reminder (User: user) in mail body" do
@user_notifier.body.should_match /Username: user/
end

specify "should contain password reminder (Password: password) in mail body" do
@user_notifier.body.should_match /Password: password/
end

specify "should contain user activation url (http://YOURSITE/activate/code) in mail body" do
@user_notifier.body.should_match %r{http://YOURSITE/activate/code}
end
end

context "A UserNotifier on activation" do
setup do
set_mailer_in_test

@user = mock("user")
@user.stub!(:email).and_return('user@test.com')
@user.stub!(:login).and_return('user')

@user_notifier = UserNotifier.create_activation(@user)
end

specify "should set @recipients to user's email" do
@user_notifier.to.should_eql ["user@test.com"]
end

specify "should set @subject to [YOURSITE] Your account has been activated!" do
@user_notifier.subject.should_eql "[YOURSITE] Your account has been activated!"
end

specify "should set @from to ADMINEMAIL" do
@user_notifier.from.should_eql ["ADMINEMAIL"]
end

specify "should contain login reminder (user,) in mail body" do
@user_notifier.body.should_match %r{user,}
end

specify "should contain website url (http://YOURSITE/) in mail body" do
@user_notifier.body.should_match %r{http://YOURSITE/}
end
end





require File.dirname(__FILE__) + '/../spec_helper'

context "A UserObserver" do
setup do
@user = mock('user')
@user_observer = UserObserver.instance
end

specify "should call UserNotifier.deliver_signup_notification on user creation" do
UserNotifier.should_receive(:deliver_signup_notification).with(@user)
@user_observer.after_create(@user)
end

specify "should call UserNotifier.deliver_activation if user was recently activated" do
UserNotifier.should_receive(:deliver_activation).with(@user)
@user.stub!(:recently_activated?).and_return(true)
@user_observer.after_save(@user)
end

specify "should not call UserNotifier.deliver_activation if user wasn't recently activated" do
UserNotifier.should_not_receive(:deliver_activation)
@user.stub!(:recently_activated?).and_return(false)
@user_observer.after_save(@user)
end
end
   
0 请登录后投票
论坛首页 Ruby版 rails

跳转论坛:
JavaEye推荐
    快速回复 引用上一条消息 (Alt+S)