# Undefined method \`has\_selector?' for "":String

**URL:** <https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205>\
**Category:** RSpec\
**Created:** [September 19, 2010, 7:44pm UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205 "2010-09-19T19:44:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 19, 2010, 7:44pm UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/1 "2010-09-19T19:44:02Z")

</div>

I’m running rails 3.0.0 on ruby 1.9.2p0.

I’ve rspec beta 22 with Capybara 0.3.9. NO WEBRAT.

I’m unable to use 'rendered.should have\_selector(‘a’) in my view specs.

1. home/\_menu.erb should add a link to home first  
Failure/Error: rendered.should have\_selector(‘a’, ‘Home’)  
undefined method `has\_selector?’ for “”:String

I read in this forum that rspec beta 20 and above should pick Capybara  
automatically if its included in Gemfile.

In my gem file I’ve this:

group :development, :test do  
gem ‘database\_cleaner’  
gem ‘cucumber-rails’  
gem ‘cucumber’  
gem ‘rspec-rails’, “\>= 2.0.0.beta.19”  
gem ‘capybara’  
gem ‘spork’  
gem ‘launchy’ # So you can do Then show me the page  
gem ‘machinist’, ‘\>= 2.0.0.beta1’  
end

Can anyone please point out what am I missing here? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 19, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/2 "2010-09-19T20:16:36Z")

</div>

On Sep 19, 2010, at 12:44 PM, Lord Raiden wrote:

> I read in this forum that rspec beta 20 and above should pick Capybara  
> gem ‘spork’  
> gem ‘launchy’ # So you can do Then show me the page  
> gem ‘machinist’, ‘\>= 2.0.0.beta1’  
> end
> 
> Can anyone please point out what am I missing here? Thanks in advance.

Two issues:

1. Capybara matchers do not support arbitrary strings, so they do not  
work in view specs
2. Even if they did, the capybara matcher would be has\_css, not  
has\_selector

---

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 20, 2010, 12:13pm UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/3 "2010-09-20T12:13:30Z")

</div>

You should use this gem: [http://github.com/grimen/rspec\_tag\_matchers](http://github.com/grimen/rspec_tag_matchers)

- Toni

---

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 21, 2010, 11:57am UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/4 "2010-09-21T11:57:21Z")

</div>

David C. wrote:

> Two issues:
> 
> 1. Capybara matchers do not support arbitrary strings, so they do not  
> work in view specs
> 2. Even if they did, the capybara matcher would be has\_css, not  
> has\_selector

Thanks for quick reply. Sorry about my delay (I was sick and down).

I’m not an expert in these tools, so please bear with me.

Is there anyway to get Capybara to work with view:specs? May be by doing  
something Cucumber is doing to get Capybara to work with it.

Reason I’m asking is I really want to write few view specs, (outside of  
cucumber), and I would like not to use Webrat, to keep things clean.

It seems the gem Tony pointed out can help, but as I said I would rather  
not use it if there is any hack which could let me use Capybara for  
rspec view specs.

---

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 21, 2010, 1:57pm UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/5 "2010-09-21T13:57:44Z")

</div>

On Sep 21, 2010, at 4:57 AM, Lord Raiden wrote:

> I’m not an expert in these tools, so please bear with me.
> 
> Is there anyway to get Capybara to work with view:specs? May be by doing  
> something Cucumber is doing to get Capybara to work with it.

Not now. Capybara is designed to work with a Rack application. View  
specs are built on Rails’ ActionView::TestCase, which is designed to  
make an isolated call into the rendering framework, and get back a  
String. They don’t know anything about requests or sessions, so the  
Capybara matchers don’t work.

I’ve talked to Jonas about extracting the matchers so they can work with  
arbitrary Strings. He’s interested in the idea, but it won’t likely  
happen for a while due to other priorities. I’ve added an issue [1],  
which you are welcome to comment on and follow.

> Reason I’m asking is I really want to write few view specs, (outside of  
> cucumber), and I would like not to use Webrat, to keep things clean.

Why would Webrat be any less clean in this case? The matchers are pretty  
much the same, and that’s all you need. Are you using Capybara in  
controller specs? If not, I’d recommend using Webrat with RSpec and  
Capybara with Cucumber.

HTH,  
David

---

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 21, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/6 "2010-09-21T14:01:15Z")

</div>

On Sep 21, 2010, at 6:55 AM, David C. wrote:

> > Thanks for quick reply. Sorry about my delay (I was sick and down).  
> > Reason I’m asking is I really want to write few view specs, (outside of  
> > cucumber), and I would like not to use Webrat, to keep things clean.
> 
> Why would Webrat be any less clean in this case? The matchers are pretty much the same, and that’s all you need. Are you using Capybara in controller specs? If not, I’d recommend using Webrat with RSpec and Capybara with Cucumber.
> 
> HTH,  
> David
> 
> > It seems the gem Tony pointed out can help, but as I said I would rather  
> > not use it if there is any hack which could let me use Capybara for  
> > rspec view specs.

Forgot to include the issue 🙂

> **[Issues · teamcapybara/capybara](https://github.com/teamcapybara/capybara/issues#issue/139)**
>
> Acceptance test framework for web applications. Contribute to teamcapybara/capybara development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 21, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/7 "2010-09-21T16:00:06Z")

</div>

El 21/09/2010, a las 13:55, David C.  
escribió:

> On Sep 21, 2010, at 4:57 AM, Lord Raiden wrote:
> 
> > David C. wrote:
> 
> > Reason I’m asking is I really want to write few view specs, (outside of  
> > cucumber), and I would like not to use Webrat, to keep things clean.
> 
> Why would Webrat be any less clean in this case? The matchers are pretty much the same, and that’s all you need. Are you using Capybara in controller specs? If not, I’d recommend using Webrat with RSpec and Capybara with Cucumber.

I think the issue is that even though they are effectively the same in  
terms of their capabilities (ie. they both wrap Nokogiri AFAIK), the  
names of the matchers are slightly different so you end up having to use  
two different sets of language depending on whether you’re writing view  
specs or acceptance specs. (This is probably less of an issue for  
Cucumber users because the Cucumber steps are another layer of  
abstraction over the top of the matchers, but if you’re a Steak user,  
then you’re always working directly with the matcher APIs).

I’m using Steak + Capybara right now for acceptance specs and Webrat for  
my view specs, but I’d prefer to be able to use Capybara matchers for  
everything in order to avoid the duplication of two very similar but  
slightly different APIs.

Cheers,  
Wincent

---

<div class="post-metadata">

**Author:** ![lord\_raiden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@lord\_raiden](https://www.ruby-forum.com/u/lord_raiden)\
**Post date:** [September 22, 2010, 5:11am UTC](https://www.ruby-forum.com/t/undefined-method-has-selector-for-string/195205/8 "2010-09-22T05:11:16Z")

</div>

> I’d prefer to be able to use Capybara matchers for  
> everything in order to avoid the duplication of two very similar but  
> slightly different APIs.
> 
> Cheers,  
> Wincent

Wincent pointed out my exact feelings on the matter (Probably in a  
better way than I could have.)

But I’m willing to follow David’s advice if that’s the only way for now,  
and it works without clashes.

/Thanks guys.
