【问题标题】:Webrat Mechanize outside of RailsRails 之外的 Webrat 机械化
【发布时间】:2010-01-11 12:45:13
【问题描述】:

我正在尝试在一个独立的脚本中使用 Webrat 来自动化一些网页浏览。如何让 assert_contain 方法工作?

require 'rubygems'
require 'webrat'

include Webrat::Methods
include Webrat::Matchers

Webrat.configure do |config|
  config.mode = :mechanize
end

visit 'http://gmail.com'
assert_contain 'Welcome to Gmail'

我收到此错误

/usr/lib/ruby/gems/1.8/gems/webrat-0.6.0/lib/webrat/core/matchers/have_content.rb:57:in 'assert_contain': undefined method assert' for #<Object:0xb7e01958> (NoMethodError)

【问题讨论】:

    标签: ruby mechanize webrat


    【解决方案1】:

    assert_contain 和其他断言是测试/单元的方法,尝试要求它并从测试方法内部使用 webrat:

    require 'test/unit'
    
    class TC_MyTest < Test::Unit::TestCase
      def test_fail
        assert(false, 'Assertion was false.')
      end
    end
    

    无论如何我还没有测试过,但是如果你感兴趣的话,我有一个适用于 rspec 的 spec_helper:

    require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
    require 'spec/rails'
    
    require "webrat"
    
    Webrat.configure do |config|
      config.mode = :rails
    end
    
    module Spec::Rails::Example
      class IntegrationExampleGroup < ActionController::IntegrationTest
    
       def initialize(defined_description, options={}, &implementation)
         defined_description.instance_eval do
           def to_s
             self
           end
         end
    
         super(defined_description)
       end
    
        Spec::Example::ExampleGroupFactory.register(:integration, self)
      end
    end
    

    加上一个规格:

    # remember to require the spec helper
    
    describe "Your Context" do
    
      it "should GET /url" do
        visit "/url"
        body.should =~ /some text/
      end
    
    end
    

    试一试,我发现它非常有用(比黄瓜和周围的其他蔬菜更有用),因为不需要文本规范(功能)而不是我最喜欢的代码规范。

    ps 你需要rspec gem,它会安装“spec”命令来执行你的规范。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      相关资源
      最近更新 更多