2010-01-11 48 views
2

我正在嘗試在獨立腳本中使用Webrat來自動化某些Web瀏覽。我如何使assert_contain方法起作用?Webrat在Rails之外進行機械化

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)

回答

2

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'命令來執行你的規格。