有没有办法用test :: unit Ruby来加载gems

时间:2011-08-13 12:09:52

标签: ruby unit-testing testunit

我正在尝试使用test :: unit进行测试,我试图测试的框架需要特定的gem(rhodes)

任何人都可以建议我在运行测试时如何加载宝石

更新::错误消息

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- rho (LoadError)
        from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from ../../app/Settings/controller.rb:1
        from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from ../test_helper.rb:4
        from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from test_settings.rb:4

我的单元测试使用require包含test_helper.rb,其中包含以下

$: << "../../app"
require 'rubygems'
require 'rhodes'
require 'test/unit'
require 'Settings/controller'

设置/控制器位于应用中且包含

require 'rho'
require 'rho/rhocontroller'
require 'rho/rhoerror'
require 'helpers/browser_helper'

class SettingsController < Rho::RhoController
  include BrowserHelper

  def index
    @msg = @params['msg']
    render
  end

  def login
    @msg = @params['msg']
    render :action => :login, :back => '/app'
  end

  def login_callback
    errCode = @params['error_code'].to_i
    if errCode == 0
      # run sync if we were successful
      WebView.navigate Rho::RhoConfig.options_path
      SyncEngine.dosync
    else
      if errCode == Rho::RhoError::ERR_CUSTOMSYNCSERVER
        @msg = @params['error_message']
      end

      if !@msg || @msg.length == 0   
        @msg = Rho::RhoError.new(errCode).message
      end

      WebView.navigate ( url_for :action => :login, :query => {:msg => @msg} )
    end  
  end

  def do_login
    if @params['login'] and @params['password']
      begin
        SyncEngine.login(@params['login'], @params['password'], (url_for :action => :login_callback) )
        render :action => :wait
      rescue Rho::RhoError => e
        @msg = e.message
        render :action => :login
      end
    else
      @msg = Rho::RhoError.err_message(Rho::RhoError::ERR_UNATHORIZED) unless @msg && @msg.length > 0
      render :action => :login
    end
  end

  def logout
    SyncEngine.logout
    @msg = "You have been logged out."
    render :action => :login
  end

  def reset
    render :action => :reset
  end

  def do_reset
    Rhom::Rhom.database_full_reset
    SyncEngine.dosync
    @msg = "Database has been reset."
    redirect :action => :index, :query => {:msg => @msg}
  end

  def do_sync
    SyncEngine.dosync
    @msg =  "Sync has been triggered."
    redirect :action => :index, :query => {:msg => @msg}
  end
end

1 个答案:

答案 0 :(得分:0)

编辑2:查看代码后,我认为以下内容应该有效:

从test_helper

移动线条
require 'rubygems'
require 'rhodes'

到设置/控制器的最顶层

或者您是否考虑在设置/拆卸期间动态加载它(可能是为了避免冲突的依赖关系等)?

编辑:我写了一个快速的例子来测试围绕Watir类的简单包装器(用于IE浏览器的UI操作器)。

require 'rubygems'
require 'watir'

require 'test/unit'

class WatirWrapper
    def initialize()
        @browser = Watir::IE.new()
    end

    def method_missing(sym, *args, &block)
        @browser.send(sym, *args, &block)
    end
end

class WatirWrapperTest < Test::Unit::TestCase
    def test_goto
        @ww = WatirWrapper.new()
        @ww.goto('http://www.google.com/')
        assert_equal('http://www.google.com/', @ww.url())
    end
end