Webrat在Rails之外进行机械化

时间:2010-01-11 03:39:42

标签: ruby mechanize webrat

我正在尝试在独立脚本中使用Webrat来自动化一些Web浏览。如何使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)

1 个答案:

答案 0 :(得分:2)

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

require 'test/unit'

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

无论如何我还没有测试过,但如果你对此感兴趣的话,我有一个有效的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'命令来执行你的规范。