[ANN] rspec_hpricot_matchers 1.0: have_tag on hpricot

Hello everyone,

A few weeks ago, I put together a little project that provides a
have_tag() matcher look-alike that can be used outside of Rails
projects, backed by Hpricot, which I very creatively named
rspec_hpricot_matchers. It’s not a drop-in replacement for
rspec_on_rails’ have_tag(): no substitution values, Hpricot’s CSS
selectors aren’t identical to assert_select’s, and with_tag() has been
replaced by nested calls to have_tag(). But it’s pretty close and even
enjoys a few benefits over the standard implementation.

I finally got around to registering a project on RubyForge and pushing
a gem, so it is now available with just:

gem install rspec_hpricot_matchers

If you’re interested, the source is hosted on github at:

GitHub - pd/rspec_hpricot_matchers: have_tag() without Rails' assert_select()

I’m using this both in isolated XML-related libraries and in my Rails
projects, and I haven’t come up with any features I want lately, so
it’s reasonably stable in my experience. Usage examples are available
in the README, and the specs are simple enough to be another good set
of examples.

Suggestions, patches and angry flames about list spamming are all
welcome.

Thanks

Kyle

Hi all! I’ve been through the RSpec Stories PeepCode and some various
other readings online, and now I’m trying to build a dummy application
of the traditional style: log in, make posts, add comments.

So I’ve run the scaffold for posts/comments and the script for
RestfulAuthentication so I have the users and SessionController and
all that set up in this project. Now I’ve written this story:

http://pastie.caboo.se/pastes/171140

In it I’m trying to do a post to “/session/create” in order to log
in. I figure I want to be able to call this from an Story so that I
can reuse it for different scenarios when I want to test things
requiring login.

What am I doing wrong here?

Thanks!
Glenn

On Mar 26, 2008, at 4:02 PM, Glenn F. wrote:

In it I’m trying to do a post to “/session/create” in order to log
in. I figure I want to be able to call this from an Story so that I
can reuse it for different scenarios when I want to test things
requiring login.

What am I doing wrong here?

Glenn,

please include a pastie that shows the error you are getting or the
behavior that isn’t working.

cr

On Mar 26, 2008, at 6:28 PM, Chuck R. wrote:

all that set up in this project. Now I’ve written this story:
Glenn,

please include a pastie that shows the error you are getting or the
behavior that isn’t working.

cr

Oh sorry about that… it was there before I promise :slight_smile:

FAILURES:
1) User Stuff (Create a new user) FAILED
Spec::Expectations::ExpectationNotMetError: expected redirect? to
return true, got false
stories/user_story.rb:34:in `I should be logged in’
stories/user_story.rb:31
stories/user_story.rb:3

On 26 Mar 2008, at 19:08, Kyle H. wrote:

I finally got around to registering a project on RubyForge and pushing
a gem, so it is now available with just:

gem install rspec_hpricot_matchers

Hi Kyle

Thanks for releasing this as a gem, makes it much easier to get hold
of. The only deficiency in it that bugs me is the poor error
reporting for nested specs, but that would be pretty tough to fix.
(I’d love to think I could add that but not sure I will get time.)

I still think this should be in RSpec core though, hint hint
David ;o) Is there any chance it will make it in? Making assertions
about XML data is pretty common doing both web-based and non-web-based
apps. Plus of course there is the little issue that Rails’
assert_select doesn’t actually work, which is generally considered a
requirement for a testing library.

Ashley

It actually should do a redirect, but checking for response.should
be_success also fails. The request isn’t actually being made. I just
can’t figure out why.

Glenn

Hi Glen,

I do not mean to insult you, but is /session/create actually doing a
redirect? A similar rspec test works for me.


Matt Berther

On Wed, Mar 26, 2008 at 7:50 PM, Ashley M.
[email protected] wrote:

Thanks for releasing this as a gem, makes it much easier to get hold
of. The only deficiency in it that bugs me is the poor error
reporting for nested specs, but that would be pretty tough to fix.
(I’d love to think I could add that but not sure I will get time.)

I still think this should be in RSpec core though, hint hint
David ;o) Is there any chance it will make it in?

Not likely. Not because it isn’t awesome. I’ll probably be using it
myself. But every good idea for a matcher library doesn’t belong in
rspec. In fact, the whole point of creating such a simple protocol for
matchers was to encourage external libraries.

I think it’s fine just as it is.

FWIW,
David

Making assertions
about XML data is pretty common doing both web-based and non-web-based
apps. Plus of course there is the little issue that Rails’
assert_select doesn’t actually work, which is generally considered a
requirement for a testing library.

That was it, thank you! Must have been a long day, I can’t believe I
didn’t spot that :slight_smile:

On 27/03/2008, David C. [email protected] wrote:

Not likely. Not because it isn’t awesome. I’ll probably be using it
myself. But every good idea for a matcher library doesn’t belong in
rspec. In fact, the whole point of creating such a simple protocol for
matchers was to encourage external libraries.

I think it’s fine just as it is.

In that case, would a central repository of re-usable matchers be a good
idea? (Maybe just a page on rspec.info?)

Ashley

On Wed, Mar 26, 2008 at 5:02 PM, Glenn F. [email protected] wrote:

Hi all! I’ve been through the RSpec Stories PeepCode and some various
other readings online, and now I’m trying to build a dummy application
of the traditional style: log in, make posts, add comments.

So I’ve run the scaffold for posts/comments and the script for
RestfulAuthentication so I have the users and SessionController and
all that set up in this project. Now I’ve written this story:

Parked at Loopia

Shouldn’t that be a post to “/sessons/”?

On Mar 27, 2008, at 12:27 PM, “Ashley M.”
<[email protected]

wrote:

good idea? (Maybe just a page on rspec.info?)
Definitely. We’re going to set up a wiki or some such. I’m interested
in a place to list matcher libs, plugins, and projects that are using
rspec as well.

Soon! Maybe :wink:

Cheers,
David

+1

On Thu, Mar 27, 2008 at 12:27 PM, Ashley M. <

Just thought I’d send out a little update on using this library. Great!
We
converted our tests over to using it, and I’m very happy with it. Good
job,
Kyle! Keep up the good work.
It did take a bit of time to convert (using @ in attributes, for
example),
but it was well worth it.

-Corey

On Wed, Mar 26, 2008 at 3:08 PM, Kyle H.
[email protected]

+2.74

On Thu, 2008-03-27 at 12:49 -0400, Corey H. wrote:

            simple protocol for

rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Jonathan L.
http://jonathanleighton.com/

Corey H. wrote:

A few weeks ago, I put together a little project that provides a
have_tag() matcher look-alike that can be used outside of Rails
projects, backed by Hpricot, which I very creatively named
rspec_hpricot_matchers.

I bet it can easily do something like this (if you rspeckers will excuse
the
dreaded word “assert”!):

def assert_all_embedded_images
assert_any_xpath :img do |img|
assert_public_image_file img[:src]
false # :keep looping!
end
end

def test_find_all_embedded_images
get :some_action # a Rails functional test
assert_all_embedded_images
end

def assert_public_image_file(src, line = nil)
src.sub!(/^//, ‘’)
# RailsRoot = Pathname.new(RAILS_ROOT)
image_file = RailsRoot + ‘public’ + src
# this file must be where the server and browser can see it
assert_file(line){ image_file }
end

Those assertions scan every image in a page and ensure a file with the
correct
name appears in ./public/images/. (Our artists have recently been
sending us new
assets, and I didn’t feel like manually checking we copied in all the
right
images…)

The :img is a shortcut for the XPathic ‘descendent-or-self::img’, and
the
img[:src] is naturally a shortcut for the attribute.

Those assertions - assert_xpath, assert_any_xpath, etc - work
interchangeably
with REXML, Hpricot, or Libxml for their parser. You just pick the one
you want
by adding to your setup() a call like invoke_rexml, invoke_hpricot, or
invoke_libxml. Each provides various trades-off, but at work we have
fixated on
Libxml, naturally, because we use hundreds of these assertions, so we
need its
speed. Also, Libxml strictly enforces the Transitional XHTML type that
appears
in all our DOCTYPEs. (gem install assert_xpath, but I haven’t documented
the
libxml variant yet…)

My little trick with the image files is only the start of assert_xpath’s
abilities. You can also call it from inside an rspec; you just have to
include
its modules. And, like any of my assertions, it provides a detailed,
comprehensive diagnostic if it fails.


Phlip