Forum: RSpec Testing for the number of occurrences of a word

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bart Z. (Guest)
on 2009-03-10 14:50
(Received via mailing list)
Hey list,

Quick question: How can I check that a given word appears a number of
times on a page?
The page in question includes some XML, rendered with &lts and &gts
and I want to test that a given node exists 10 times on that page.

thanks!
bartz
Zach D. (Guest)
on 2009-03-10 15:11
(Received via mailing list)
On Tue, Mar 10, 2009 at 8:16 AM, Bart Z.
<removed_email_address@domain.invalid> wrote:
> Hey list,
>
> Quick question: How can I check that a given word appears a number of times
> on a page?
> The page in question includes some XML, rendered with &lts and &gts and I
> want to test that a given node exists 10 times on that page.

You want to make sure you have 10 nodes? Or that text shows up 10
times inside of any node?

You can utilize Webrat's #have_selector to count the nodes, ie:

        response.should have_selector("li", :count => 3)


>
> thanks!
> bartz
> _______________________________________________
> rspec-users mailing list
> removed_email_address@domain.invalid
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Zach D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com
Aslak H. (Guest)
on 2009-03-10 15:57
(Received via mailing list)
On Tue, Mar 10, 2009 at 1:16 PM, Bart Z.
<removed_email_address@domain.invalid>wrote:

> Hey list,
>
> Quick question: How can I check that a given word appears a number of times
> on a page?
> The page in question includes some XML, rendered with &lts and &gts and I
> want to test that a given node exists 10 times on that page.
>

Here are a couple of ways to do it:
http://lmgtfy.com/?q=ruby+count+occurrence+words

count = # one of the solutions above
count.should == 10

Cheers,
Aslak
Bart Z. (Guest)
on 2009-03-10 17:40
(Received via mailing list)
On 10 mrt 2009, at 14:05, aslak hellesoy wrote:

>
> Here are a couple of ways to do it: http://lmgtfy.com/?q=ruby+count+occurrence+words
>
> count = # one of the solutions above
> count.should == 10

Argh, thanks to both of you.
I was thinking too difficult, along the lines of have_tag('my_div',
'word', 10)...

cheers,
bartz
Bart Z. (Guest)
on 2009-03-10 22:35
(Received via mailing list)
On 10 mrt 2009, at 16:37, Bart Z. wrote:

>>
>> Here are a couple of ways to do it: http://lmgtfy.com/?q=ruby+count+occurrence+words
>>
>> count = # one of the solutions above
>> count.should == 10
>
> Argh, thanks to both of you.
> I was thinking too difficult, along the lines of have_tag('my_div',
> 'word', 10)...

In case anyone was wondering how to do this:

Then "I should see the last $num statuses of the user" do |num|
   matches = response.body.scan(/&lt;record&gt;/)
   matches.length.should == num.to_i
end
This topic is locked and can not be replied to.