Testing layout

36 views
Skip to first unread message

Christian Guimarães

unread,
Aug 18, 2012, 5:34:11 PM8/18/12
to sina...@googlegroups.com
Hello all,

How do you, guys, handle layout tests? Creating a file like 'layout_spec.rb'?

I mean, test things that will appear in all rendered routes, like header links/redirections, menu item links and footer links?

Cheers.

-- 
Christian Guimarães

Josh Cheek

unread,
Aug 19, 2012, 1:53:15 AM8/19/12
to sina...@googlegroups.com

I don't test presentation, there's not a great way to do it. If you _really_ need to do this, there are tools like Mogotest http://mogotest.com/ which are intended for that purpose.

Aside from presentation, I have tested correctness of generated HTML and XML, but this is a pretty rare need. If link veracity were a concern, I'd probably try to find a spider (I did once write one to make sure links all still worked after discovering some broken ones on my company's blog due to migrating to a different framework).

-Josh

Carlos Eduardo L. Lopes

unread,
Aug 19, 2012, 2:19:28 PM8/19/12
to sina...@googlegroups.com
I test them using capybara.. In my projects i always have a 'layout_links_spec.rb' file.

--
Carlos Eduardo L. Lopes
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To post to this group, send email to sina...@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.

Iain Barnett

unread,
Aug 23, 2012, 6:08:48 PM8/23/12
to sina...@googlegroups.com

On 18 Aug 2012, at 22:34, Christian Guimarães wrote:

> Hello all,
>
> How do you, guys, handle layout tests? Creating a file like 'layout_spec.rb'?
>
> I mean, test things that will appear in all rendered routes, like header links/redirections, menu item links and footer links?
>

I use Rack Test and RSpec's shared examples and shared contexts. Put them in spec/support/shared/ and then refer to them like:

describe "Public pages" do

describe "Home page", :type => :request do
let(:page) { "/" }
before { get page }
include_context "All pages"
subject { last_response.body }

context "First visit to home page" do
it_should_behave_like "Any public page"
# more stuff follows...


Then it's easy to check for things like a sign in button on pages that are public and you haven't signed in yet.

I also separate everything out of the config.ru that I can (everything except `run`, pretty much) into a config.rb with a modular style app, and that makes it a lot easier to include in tests:

shared_context "All pages" do
include Rack::Test::Methods
include IainsBlog # the modular app from config.rb
let(:app){ IainsBlog.app }
include Boilerplate::RSpec::Helpers # some helpers I cooked up
end

shared_examples_for "Any public page" do
subject { last_response }
it { should be_ok }
it { subject.body.should include_link( uri "/" ).within("header[@role=banner]") }
end


You can see an example of what I mean (about the config.rb) in the examples directory of Sinatra::Partial:

https://github.com/yb66/Sinatra-Partial/tree/develop/examples/app_no_underscores

It meant I was able to pull in the examples to test against in a loop. When I'm doing a site I use it with the shared_context.


Regards,
Iain
Reply all
Reply to author
Forward
0 new messages