2

スタンドアロン スクリプトで 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)

4

1 に答える 1

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 を実行するための「spec」コマンドがインストールされます。

于 2010-01-11T04:45:13.460 に答える