<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Live in code &#187; rspec</title>
	<atom:link href="http://www.agatezone.cn/code/tag/rspec/feed" rel="self" type="application/rss+xml" />
	<link>http://www.agatezone.cn/code</link>
	<description>agate&#039;s tech blog</description>
	<lastBuildDate>Tue, 20 Jul 2010 05:35:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ubuntu 下 rails/rspec/autotest 自动测试环境搭建</title>
		<link>http://www.agatezone.cn/code/archives/257</link>
		<comments>http://www.agatezone.cn/code/archives/257#comments</comments>
		<pubDate>Fri, 17 Oct 2008 05:18:15 +0000</pubDate>
		<dc:creator>agate</dc:creator>
				<category><![CDATA[开发环境]]></category>
		<category><![CDATA[程序编码]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.agatezone.cn/code/?p=257</guid>
		<description><![CDATA[羡不羡慕那些 rails 视频在 MAC OS X 下面帅气的自动跳出绿色和红色的自动测试提示呢? 当然如果你是 MAC 用户就不用羡慕了哈!(比如我就不羡慕嘿嘿) 但是大部分朋友都是 PC 呀! 没有 MAC 下的 Growl 作为 UI 提示工具怎么办呢? 没事啦! 这些我们都好解决, Ubuntu 下面我们可以用 rnotify 这个 ruby gnome 的消息提示工具. Windows 下我们可以用 ruby-snarl 这个消息提示工具啦! 当然还有 AutoIt 这类辅助工具! 这里我们先说 Ubuntu 这样的 GNOME 环境中的解决方案.
首先是安装 autotest, rspec, rspec_rails 三个插件
$sudo gem install autotest
$rails hello
$cd hello
$./script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec
$./script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails
$./script/generate rspec

这样便安装好最基础的 [...]]]></description>
			<content:encoded><![CDATA[<p>羡不羡慕那些 rails 视频在 MAC OS X 下面帅气的自动跳出绿色和红色的自动测试提示呢? 当然如果你是 MAC 用户就不用羡慕了哈!(比如我就不羡慕嘿嘿) 但是大部分朋友都是 PC 呀! 没有 MAC 下的 Growl 作为 UI 提示工具怎么办呢? 没事啦! 这些我们都好解决, Ubuntu 下面我们可以用 rnotify 这个 ruby gnome 的消息提示工具. Windows 下我们可以用 ruby-snarl 这个消息提示工具啦! 当然还有 AutoIt 这类辅助工具! 这里我们先说 Ubuntu 这样的 GNOME 环境中的解决方案.</p>
<p>首先是安装 autotest, rspec, rspec_rails 三个插件<br />
<code>$sudo gem install autotest<br />
$rails hello<br />
$cd hello<br />
$./script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec<br />
$./script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails<br />
$./script/generate rspec<br />
</code><br />
这样便安装好最基础的 BDD 环境了. 手动在你的 rails 目录下键入<br />
<code>$./script/autospec<br />
<span style="color: #339966;">这是这个自动测试不是在后台运行的, 因为他要显示信息嘛!<br />
所以最小化这个终端窗口吧, 或者新开一个 Tab 来继续操纵你的 rails 应用.</span></code><br />
就可以启动针对此 rails 应用的持续测试功能了. 让我们来试试看! 如何工作, 在终端中你的 rails 目录下键入:<br />
<code>$./script/generate rspec_scaffold post title:string<br />
$rake db:migrate<br />
$rake db:test:clone</code><br />
看看你的那个自动测试窗口吧! (如果你的小机器不太快, 这个测试窗口还和原来一样, 那么稍等一会会, 他马上就运行咯!) 你大概会看到类似的结尾结果:<br />
<img src="http://www.agatezone.cn/code/wp-content/uploads/2008/10/ubuntu-autotest-terminal.png" alt="" /><br />
哈! 是不是很有成就感? (当时我满有的...) 呵呵, 但是这个只是在终端中看到的, 难不成要我们每次保存了新的东西都要光顾我们的终端信息么? Mac 下有 Growl 来做桌面 UI 气泡提示. 在 Ubuntu 这样的 GNOME 下我们可以使用 ruby-notify 来实现. 首先必须安装 ruby-gnome2 / libnotify-dev 和 ruby-libnotify 这些组件依赖. (<span style="color: #ff0000;">其中 ruby-libnotify 需要手动安装</span>)<br />
<code>$sudo apt-get install ruby-gnome2 libnotify-dev</code><br />
以上安装好了 ruby-gnome2 和 libnotify-dev , 至于 ruby-libnotify 可以到其<a href="http://ruby-libnotify.rubyforge.org" onclick="pageTracker._trackPageview('/outgoing/ruby-libnotify.rubyforge.org?referer=');">官方网站</a>下载手动其<span style="color: #ff0000;">源代码</span>编译安装, 当前最新版本为 <a href="http://rubyforge.org/frs/?group_id=1911" onclick="pageTracker._trackPageview('/outgoing/rubyforge.org/frs/?group_id=1911&amp;referer=');">0.3.3</a>, 在其下载页面我下好了 ruby-libnotify-0.3.3.tar.bz2 文件, 将其解压开好. 使用终端进入其目录中执行:<br />
<code>$ruby extconf.rb<br />
$sudo make<br />
$sudo make install</code><br />
这样就安装好 ruby-libnotify 了! 其实就是用它来代替 Mac 下的 Growl 啦! 接着建立 ~/.autotest 文件并填入如下代码.</p>
<pre class="brush: ruby;">
require 'rnotify'
require 'gtk2'

module Autotest::RNotify
  class Notification
    attr_accessor :verbose, :image_root, :tray_icon, :notification,
      :image_pass, :image_pending, :image_fail,
      :image_file_pass, :image_file_pending, :image_file_fail,
      :status_image_pass, :status_image_pending, :status_image_fail

    def initialize(timeout = 5000,
        image_root = &quot;#{ENV['HOME']}/.autotest_images&quot; ,
        verbose = false)
      self.verbose = verbose
      self.image_root = image_root
      self.image_file_pass = &quot;#{image_root}/pass.png&quot;
      self.image_file_pending = &quot;#{image_root}/pending.png&quot;
      self.image_file_fail = &quot;#{image_root}/fail.png&quot;

      raise(&quot;#{image_file_pass} not found&quot;) unless File.exists?(image_file_pass)
      raise(&quot;#{image_file_pending} not found&quot;) unless File.exists?(image_file_pending)
      raise(&quot;#{image_file_fail} not found&quot;) unless File.exists?(image_file_fail)

      puts 'Autotest Hook: loading Notify' if verbose
      Notify.init('Autotest') || raise('Failed to initialize Notify')

      puts 'Autotest Hook: initializing tray icon' if verbose
      self.tray_icon = Gtk::StatusIcon.new
      tray_icon.pixbuf = Gdk::Pixbuf.new(image_file_pending,22,22)
      tray_icon.tooltip = 'RSpec Autotest'

      puts 'Autotest Hook: Creating Notifier' if verbose
      self.notification = Notify::Notification.new('X', nil, nil, tray_icon)

      notification.timeout = timeout

      Thread.new { Gtk.main }
      sleep 1
      tray_icon.embedded? || raise('Failed to set up tray icon')
    end

    def notify(icon, tray, title, message)
      notification.update(title, message, nil)
      notification.pixbuf_icon = icon
      tray_icon.tooltip = &quot;Last Result: #{message}&quot;
      tray_icon.pixbuf = tray
      notification.show
    end

    def passed(title, message)
      self.image_pass ||= Gdk::Pixbuf.new(image_file_pass, 48, 48)
      self.status_image_pass ||= Gdk::Pixbuf.new(image_file_pass, 22, 22)
      notify(image_pass, status_image_pass, title, message)
    end

    def pending(title, message)
      self.image_pending ||= Gdk::Pixbuf.new(image_file_pending, 48, 48)
      self.status_image_pending ||= Gdk::Pixbuf.new(image_file_pending, 22, 22)
      notify(image_pending, status_image_pending, title, message)
    end

    def failed(title, message)
      self.image_fail ||= Gdk::Pixbuf.new(image_file_fail, 48, 48)
      self.status_image_fail ||= Gdk::Pixbuf.new(image_file_fail, 22, 22)
      notify(image_fail, status_image_fail, title, message)
    end

    def quit
      puts 'Autotest Hook: Shutting Down...' if verbose
      #Notify.uninit
      Gtk.main_quit
    end
  end

  Autotest.add_hook :initialize do |at|
    @notify = Notification.new
  end

  Autotest.add_hook :ran_command do |at|
    results = at.results.last

    unless results.nil?
      output = results[/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/]
      if output
        failures = $~[2].to_i
        pending = $~[4].to_i
      end

      if failures &gt; 0
        @notify.failed(&quot;Tests Failed&quot;, output)
      elsif pending &gt; 0
        @notify.pending(&quot;Tests Pending&quot;, output)
      else
        unless at.tainted
          @notify.passed(&quot;All Tests Passed&quot;, output)
        else
          @notify.passed(&quot;Tests Passed&quot;, output)
        end
      end
    end
  end

  Autotest.add_hook :quit do |at|
    @notify.quit
  end
end
</pre>
<p>然后呢, 这里有几个图片, 请你下载下来保存到 ~/.autotest_images 目录下(自己建立这个文件夹).<br />
<img src="http://www.agatezone.cn/code/wp-content/uploads/2008/10/pending.png" alt="" /> <img src="http://www.agatezone.cn/code/wp-content/uploads/2008/10/pass.png" alt="" /> <img src="http://www.agatezone.cn/code/wp-content/uploads/2008/10/fail.png" alt="" /><br />
(注意哦! 文件名不要变哦! 分别为 pending.png pass.png fail.png)</p>
<p>打工告成! 你可以在 rails 工程目录里头执行:<br />
<code>$./script/autospec</code><br />
不出意外的话你应该会看到如下结果<br />
<img src="http://www.agatezone.cn/code/wp-content/uploads/2008/10/ubuntu-autotest-notify.png" alt="" /></p>
<p>Tips:<br />
1. ~/.autotest 这个文件可以定制 autotest 的运行.<br />
2. ruby-libnotify 可以用于 GNOME 中作为消息提示.(<span style="color: #ff0000;">注意哦! 用源代码编译安装哦!</span>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agatezone.cn/code/archives/257/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
