---
title: "在view spec中用Capybara.string的结果替代rendered（Capybara版本0.4.12）"
author: "yuan"
date: "2011-05-10T10:12:09Z"
tags: ["capybara", "rspec", "测试"]
categories: ["编程语言"]
slug: "view-spec-capybara-string-rendered-capybara-0-4-12"
draft: false
lastmod: "2011-07-14T10:37:53Z"
id: 1038632
url: "https://yuan.iteye.com/blog/1038632"
---

问题关键字：undefined method `has_selector?' for #<String:0x1034ecfb0>

<span style="color:red">**版本：
Capybara(0.4.12)
RSpec-Rails(2.5.0)

补充：刚试了一下最新版本的RSpec-Rails2.6.0.rc6和Capybara1.0.0.beta1，已经把这个问题给解决了……**</span>

google一番后：
<https://github.com/rspec/rspec-rails/issues/213>
<https://github.com/jnicklas/capybara/issues/139>
<https://github.com/rspec/rspec-rails/issues/302>

最后：

在spec_helper.rb中加入如下代码：



```ruby
if defined?(Capybara) && !defined?(Webrat)
  module ActionView::TestCase::Behavior
    alias old_rendered rendered

    def rendered
      Capybara.string old_rendered
    end
  end
end
```