views:

240

answers:

1

I'm trying to use Webrat in a standalone script to automate some web browsing. How do I get the assert_contain method to work?

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'

I get this error

/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  A: 

assert_contain and other assertions are methods of test/unit, try to require it and use webrat from inside a test method:

require 'test/unit'

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

anyway i haven't tested it but I have a working spec_helper for rspec if this can interest you:

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

plus a spec:

# remember to require the spec helper

describe "Your Context" do

  it "should GET /url" do
    visit "/url"
    body.should =~ /some text/
  end

end

give it a try I found it very useful (more than cucumber and the other vegetables around) when there is no need to Text specs (features) instead of Code specs, that I like the most.

ps you need the rspec gem and it installs the 'spec' command to execute your specs.

makevoid