首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webrat在Rails之外实现机械化

Webrat在Rails之外实现机械化
EN

Stack Overflow用户
提问于 2010-01-11 11:39:42
回答 1查看 1.2K关注 0票数 2

我正在尝试在一个独立的脚本中使用Webrat来自动化一些web浏览。如何让assert_contain方法工作?

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

发布于 2010-01-11 12:45:13

assert_contain和其他断言是测试/单元的方法,请尝试从测试方法内部请求并使用webrat:

代码语言:javascript
复制
require 'test/unit'

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

无论如何,我还没有测试过它,但是如果你对此感兴趣的话,我有一个适用于rspec的spec_helper:

代码语言:javascript
复制
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

外加一个规格:

代码语言:javascript
复制
# 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。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2039777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档