我正在尝试在一个独立的脚本中使用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)
发布于 2010-01-11 12:45:13
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。
https://stackoverflow.com/questions/2039777
复制相似问题