Capybara & Rspec: How can I always test for the layout so I don't have to keep rewriting the code for it?

I'm trying to stick to DRY ways of doing things with my testing. I have two layouts, public and application that show certain links and css depending on if a user is signed in or not. I'm wondering if their is a way or how I would could always test for either my public or application links and content? They use different views from my views/shared folder.

Here is an example of a test for my Welcome Page which is public:

spec/requests/pages_spec.rb

describe 'Pages' do

  describe 'Welcome Page' do

    before { visit root_path }
    response.should render_template("layouts/public")

    it 'should have public footer present' do
      find_link('Home').visible?
      find_link('Login').visible?
      find_link('Help').visible?
    end
  end

end 

Now the public footer is apart of the public layout and I have more code that belongs to this layout. My goal is to write this only once and put it inside of some method and use it for the pages/test I need it for. How would I do this? I don't want to write to see if the appropriate layout with its content is being used in every single test.

Answers


It's important to remember that RSpec is just Ruby, or more specifically it's a DSL written in Ruby targeted at testing.

So, just define a method.

describe 'Pages' do

  describe 'Welcome Page' do

    before { visit root_path }
    response.should render_template("layouts/public")

    it 'should have public footer present' do
      links_are_visible?.should be true
    end

    # Call this wherever, whenever you need it
    def links_are_visible?
      find_link('Home').visible? && find_link('Login').visible && find_link('Help').visible?
    end
  end

end 

You can also do shared examples and other things outlined in this article: http://testdrivenwebsites.com/2011/08/17/different-ways-of-code-reuse-in-rspec/


Need Your Help

How can I use Razor in non-web applications… such as a mail merge program?

c# c#-4.0 razor asp.net-mvc-3

Has anyone attempted to use Razor outside of a Web Application, such as within a C# commandline application?