[rspec-users] Controller/View User Agent Strings

David Chelimsky dchelimsky at gmail.com
Thu Feb 15 10:56:48 EST 2007


Given this in views/layouts/person.rhtml:

<%= stylesheet_link_tag('ie', :media => 'screen') if
request.user_agent && request.user_agent =~ /msie/i %>

The following passes for me:

====================================
context "layout for person w/ user_agent = msie" do
  setup do
    request.stub!(:user_agent).and_return('msie')
    render 'layouts/person'
  end

  specify "should have msie stylesheet" do
    response.should have_tag("link[href*=ie.css]")
  end

  specify "should not have firefox stylesheet" do
    response.should_not have_tag("link[href*=firefox.css]")
  end

end

context "layout for person w/ user_agent = firefox" do
  setup do
    request.stub!(:user_agent).and_return('mozilla')
    render 'layouts/person'
  end

  specify "should have no link tag" do
    response.should_not have_tag("link")
  end

end
====================================

Take a look at the assert_select cheat sheet for more info:

http://labnotes.org/svn/public/ruby/rails_plugins/assert_select/cheat/assert_select.html

have_tag just wraps that w/ rspec syntax.

Let me know if this helps.

Cheers,
David

On 2/13/07, s.ross <cwdinfo at gmail.com> wrote:
> Just checking. I guess it just seemed too easy :)
>
> What happens when this page renders is a tag like:
>
> <link href="/path/to/my/stylesheet.css?123453498">
>
> Or something like that with the Rails asset tagging. What I want to
> do is test that I'm responding to the user agent string correctly
> using a matcher that looks for the filename, but disregards the asset
> tag:
>
> response.should have_tag('link', :attributes => {:href => /
> application.css/})
> response.should_not have_tag('link', :attributes => {:href => /ie.css/})
>
> However, the "should have" expectations always pass no matter what.
> Not coincidentally, the "should_not have" ones always fail.
>
> I can see, looking at the Rails plugin source that my syntax is
> broken and that the have_tag expectation was meant more to determine
> whether the tag exists and how many times it occurs. Is there a
> matcher that peers into the tag the way the old Test::Unit assertions
> did?
>
> Thanks
>
> On Feb 13, 2007, at 4:40 PM, David Chelimsky wrote:
>
> > On 2/13/07, s.ross <cwdinfo at gmail.com> wrote:
> >> I have conditional logic in my view (I know, obligatory slap on the
> >> wrist) so that an add-on stylesheet is linked in for IE users. The
> >> code is:
> >>
> >>      <%= stylesheet_link_tag('ie', :media => 'screen') if
> >> request.user_agent && request.user_agent =~ /msie/i %>
> >>
> >> The spec is:
> >>
> >>    specify "should display index page" do
> >>      get :index
> >>      response.should_be_success
> >>    end
> >>
> >> There is no user_agent in the request object passed to the view. Now,
> >> I can change all of this by simply:
> >>
> >> setup do
> >>      request.stub!(:user_agent).and_return('msie')
> >> end
> >>
> >> but I was wondering if there was a better way?
> >
> > That seems pretty simple to me. What is bugging you about it?
> >
> >>
> >> Thanks,
> >>
> >> Steve
> >> _______________________________________________
> >> rspec-users mailing list
> >> rspec-users at rubyforge.org
> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >>
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list