开发者

Is there a way with test::unit Ruby to load gems

开发者 https://www.devze.com 2023-03-27 17:10 出处:网络
I am trying to use test::unit for testing and the framework I am trying to test requires a particular gem (rhodes)

I am trying to use test::unit for testing and the framework I am trying to test requires a particular gem (rhodes)

Can anyone suggest how I can get the gem loaded when I run my tests

Update :: Error Message

/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开发者_JAVA技巧_require.rb:31:in `require'
        from test_settings.rb:4

My unit test uses require to include test_helper.rb which contains following

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

Settings/controller lives in app and contains

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


Edit 2: after looking at your code, I believe the following should work:

Move the lines from test_helper

require 'rubygems'
require 'rhodes'

to the very top of Settings/controller

Or are you thinking of loading it dynamically during setup/teardown (possibly to avoid conflicting dependencies etc.)?

Edit: I wrote up a quick example of testing a simple wrapper around a Watir class (UI manipulator for IE browser).

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号