Forum: RSpec [Cucumber, Webrat] I should see behaves strangely with html

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.
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-01-19 00:56
Hi,

In my tests, I check to see if a given html tag such as
<title>...</title> has the expected value, sometimes it works sometimes
it tells me it couldn't find the string, although I see it printed out
in the error screen.

I think it comes form the fact that double-quotes are escaped and
slashes too, and some other strange behavior with spaces and dashes, but
I am not sure about that.

How do you get round this problem?
90e140f8553ecdec1b0d7dbf608a0561?d=identicon&s=25 Pau Cor (phylae)
on 2009-01-19 05:18
Can you please post the contents of your step definition?
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-01-19 12:14
Pau Cor wrote:
> Can you please post the contents of your step definition?

I don't have any step definition, I simply use: Then I should see
"<title>Hello world</title>, its definition is in webrat_steps.rb

However yesterday I looked in the code of a project (mephistoblog was
it?), and I noticed that they have: config.gem 'nokogiri' in
environement.rb. I don't have it, is it compulsory for correctly parsing
the response body?
90e140f8553ecdec1b0d7dbf608a0561?d=identicon&s=25 Pau Cor (phylae)
on 2009-01-19 15:21
Fernando Perez wrote:
> However yesterday I looked in the code of a project (mephistoblog was
> it?), and I noticed that they have: config.gem 'nokogiri' in
> environement.rb. I don't have it, is it compulsory for correctly parsing
> the response body?

I don't have config.gem 'nokogiri' in environement.rb, but I do have the
gem installed.


> I don't have any step definition, I simply use: Then I should see
> "<title>Hello world</title>, its definition is in webrat_steps.rb

Here is the step definition:
Then /^I should see "(.*)"$/ do |text|
  response.body.should =~ /#{text}/m
end

That works pretty much the same way you would use it in RSpec. I don't
see any webrat methods (please correct me if I am mistaken).

I think what might be tripping you up is that this step definition just
puts whatever is in quotes into a Regex. I personally think we should
avoid having a Regex in a step because most business users don't
understand them. So I often change this matcher to be

Then /^I should see "(.*)"$/ do |text|
  response.body.should =~ /#{Regexp.escape(text)}/m
end

I suggest you either try that step matcher, or you make sure your step
is properly escaping any special Regex characters. Perhaps this:
  Then I should see "\<title\>Hello world\<\/title\>"

I believe that it prints out the regex used if response.body.should =~
/#{text}/m fails. If you still can't get it to work, pasting the entire
output of you test here (maybe use Pastie.org)

Caveat: I am running version 0.1.13 so it is possible things have
changed.

HTH
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2009-01-19 19:19
(Received via mailing list)
On Mon, Jan 19, 2009 at 9:21 AM, Pau Cor <lists@ruby-forum.com> wrote:
>> I don't have any step definition, I simply use: Then I should see
> I think what might be tripping you up is that this step definition just
> puts whatever is in quotes into a Regex. I personally think we should
> avoid having a Regex in a step because most business users don't
> understand them. So I often change this matcher to be
>
> Then /^I should see "(.*)"$/ do |text|
>  response.body.should =~ /#{Regexp.escape(text)}/m
> end

IMO this should be the default,

> changed.
>
> HTH
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
This topic is locked and can not be replied to.