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