[rspec-users] How To Drive Out AJAX Functionality in a Rails View

David Chelimsky dchelimsky at gmail.com
Tue May 12 10:07:49 EDT 2009


On Tue, May 12, 2009 at 8:33 AM, Phlip <phlip2005 at gmail.com> wrote:
> Lee Longmore wrote:
>>
>> I am new to RSpec and have just started to drive out my first Rails view
>> using a spec. I have used the The RSpec Book (beta) to do the basic stuff
>> like testing for the presence of a field but I am unsure where to start for
>> driving out some AJAX functionality.
>>
>> In the view, I will have a text field that a user can enter a name into
>> and then click on a link to check the availability of this name or
>> otherwise. I plan to implement the check as an AJAX request, returning an
>> HTML snippet or 'available' or 'not available'.
>
> gem install nokogiri rkelly assert2
> ...
> require 'assert2/rjs'
>
>  specify 'a simple passing assertion works' do
>    xhr :get, :xhr_availability, :name => 'masone'
>    js = @response.body
>    js.should send_js_to(:replace_html, 'label_7', /available/)
>
>    js.should send_js_to(:replace_html, 'label_7'){
>                span /available/
>                }
>  end
>
> The first send_js_to just checks for a regexp to match the payload of the
> Element.update call. The second one uses {} to generate a Nokogiri Builder
> snippet of HTML, and match that. Here it's just span, but it could have been
> more complex HTML. Google assert_xhtml for more on that.
>
>> I would appreciate some help on what sort of examples one might write and
>> the various mocks, stubs and helpers etc that can be used. Or alternatively
>> a pointer to some existing information/tutorials on this topic.
>
> The RSpec community, in my exalted opinion, mocks too much! If you can't use
> the real thing, it's too coupled, so break it up!

Phlip,

You could just say "be careful not to mock too much," which would be
really good advice, but instead you make a generalizing and judgmental
statement like this. When's the last time you paired with someone in
the RSpec community?

Lee,

Phlip has some really great ideas about some things, and assert2 is
lovely, but based on several posts on this and other lists, I'd say he
doesn't really like RSpec, nor understand its underlying intent. So by
all means listen to his advice, and by all means take advantage of
assert2 for this particular problem, but please also understand that
he is in no way the voice of the RSpec community.

Cheers,
David


>
> --
>  Phlip
>  http://flea.sourceforge.net/resume.html
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list