`
hideto
  • 浏览: 2667115 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之Testing Incoming Email

    博客分类:
  • Ruby
阅读更多
今天来看看Rails对接收Email的测试,首先ruby script/generate mailer Receiver,Rails会自动为我们生成test/unit/receiver_test_pristine.rb:
require File.dirname(__FILE__) + '/../test_helper'
require 'receiver'

class ReceiverTest < Test::Unit::TestCase
  FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures'
  CHARSET = 'utf-8"

  include ActionMailer::Quoting

  def setup
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []

    @expected = TMail::Mail.new
    @expected.set_content_type "text", "plain", { "charset" => CHARSET }
  end

  private
    def read_fixture(action)
      IO.readlines("#{FIXTURES_PATH/receiver/#{action}")
    end

    def encode(subject)
      quoted_printable(subject, CHARSET)
    end
end

这样可以在test/fixtures/receiver目录下保存一些Email的源文件作为数据,例如下面将用到的文件有
test/fixtures/receiver/confidential_opportunity和test/fixtures/receiver/latest_screensaver这两个文件
我们可以使用Thunderbird来查看Message Source
下面定义我们的receiver:
app/modles/receiver.rb
class Receiver < ActionMailer::Base

  def receive(email)

    rating = 0

    if(email.subject + email.body =~ /opportunity/i)
      rating += 1
    end

    if email.has_attachments?
      email.attachments.each do |attachment|
        rating += 1 if attachment.original_filename =~ /zip$/i
      end
    end

    Mail.create(:subject => email.subject,
                :body => email.body,
                :sender => email.form,
                rating => rating)
  end
end

下面来写我们自己的test/unit/receiver_test.rb:
def test_fixtures_are_working
  emial_text = read_fixture("confidential_opportunity").join
  assert_match(/opportunity/i, email_text)
end

def test_incoming_email_gets_added_to_database
  count_before = Mail.count
  email_text = read_fixture("confidential_opportunity").join
  Receiver.receive(email_text)
  assert_equal(count_before + 1, Mail.count)
  assert_equal("CONFIDENTIAL OPPORTUNITY", Mail.find(:all).last.subject)
end

def test_email_containing_opportunity_rates_higher
  email-text = read_fixture("confidential_opportunity").join
  Receiver.receive(email_text)
  assert(Mail.find_by_subject("CONFIDENTIAL OPPORTUNITY").rating > 0)
end

def test_zip_file_increases_rating
  email_text = read_fixture("latest_screensaver").join
  Receiver.receive(email_text)
  assert(Mail.find_by_subject("The latest new screensaver!").rating > 0)
end

上面我们测试了接收Email以及接收带附件的Email,注意附件会编码为文本来传输,通过查看Email的源文本可以看到
分享到:
评论
2 楼 hideto 2007-05-28  
哈哈
1 楼 blackanger 2007-05-28  
看来我得每天跟进博主的blog才行呀。。。
还有,博主的头像实在太。。。我的烟瘾又犯啦。。。

相关推荐

    Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf

    综上所述,《Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf》是一个全面的资源,无论你是Rails新手还是资深开发者,都能从中学习到如何为Rails应用编写高质量的测试。从理论到实践,从单元测试...

    Rails 101 入门电子书

    《Rails 101 入门电子书》是一本非常适合初学者直接入门的书籍,它由xdite编写并出版于2014年6月10日。本书主要针对的是希望学习Ruby on Rails框架的读者,特别是那些想要从零开始掌握这项技术的新手。 #### 二、...

    rails2-sample

    MVC是Rails的核心架构之一,这一章节将详细介绍这三个组件的作用和相互关系。模型负责与数据库交互,管理数据;视图用于展示数据给用户;控制器则处理用户请求,协调模型和视图之间的操作。了解并正确运用MVC模式是...

    rails-controller-testing:将`assigns`和`assert_template`带回到您的Rails测试中

    Rails :: Controller :: Testing 这个gem将assigns给控制器测试的内容以及assert_template带回assigns控制器和集成测试的内容。 这些方法已中。 安装 将此行添加到您的应用程序的Gemfile中: gem 'rails-...

    rails指南 中文版

    Rails指南中文版是针对Ruby on Rails框架的一份详尽教程,旨在帮助开发者深入理解并熟练掌握这个强大的Web应用开发工具。Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-...

    web开发之rails最新调试通过购物车代码

    本压缩包中的"web开发之rails最新调试通过购物车代码"是关于使用Rails进行Web应用开发的一个实例,特别是针对购物车功能的实现。 购物车是电商网站的核心部分,它允许用户选择商品并保存这些选择以便后续购买。在...

    Rails之道.pdf(最新版)

    《Rails之道》详细讨论了Rails的程序代码并通过分析Rails中的代码片段来深入解释它的功能,同时,《Rails之道》部分章节也摘录了一些API文档中的内容,使读者能够快速地找到对应的API文档、相关的示例代码以及深入的...

    Rails入门教程一(翻译).pdf

    《Rails入门教程一》是针对初学者的一份详尽指南,旨在帮助读者快速掌握Ruby on Rails框架的基础知识。Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

    rails_email_preview, 在 Rails 中,预览和编辑应用程序邮件程序模板.zip

    rails_email_preview, 在 Rails 中,预览和编辑应用程序邮件程序模板 Rails 电子邮件预览 使用这里 Rails 引擎在浏览器中预览电子邮件。 兼容 Rails 4.2 。电子邮件审阅: 所有电子邮件预览的列表: 代表有两个主题...

    关于rails 3.1 cucumber-rails 1.2.0

    Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...

    rails 5 test prescriptions build a healthy codebase

    With both practical code examples and discussion of why testing works, this book starts with the most basic features delivered as part of core Ruby on Rails. Once you’ve integrated those features ...

    Rails之道.pdf 高清 带书签

    Rails之道.pdf 高清 带书签

    Rails

    标题 "Rails" 指的是 Ruby on Rails,一个开源的Web应用程序框架,它基于Ruby编程语言,遵循MVC(模型-视图-控制器)架构模式。Rails由David Heinemeier Hansson在2004年创建,其设计理念是强调代码的简洁性、DRY...

    RailsSpace

    本书通过构建一个面向Ruby社区的社交网络平台——RailsSpace,来帮助读者掌握Ruby on Rails的核心概念和技术。本书不仅适合初学者,也适合有一定基础并希望深入了解Rails框架的开发者。 #### 二、基础知识篇 #####...

    rails 2.3.2离线安装rails 2.3.2离线安装

    rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...

    ruby on rails社区网站开发源码

    Ruby on Rails,简称Rails,是由David Heinemeier Hansson创建的一个开源Web应用程序框架,它基于Ruby编程语言。这个框架以其MVC(Model-View-Controller)架构、约定优于配置(Convention over Configuration)的...

Global site tag (gtag.js) - Google Analytics