Topic: How do I test this?

I am writing a simple portfolio application and am in need of some assistance.

I have the following feature:

Feature: Viewing the Portfolio
  As one who works for a living
  I want to record to the projects that I work on
  So that the world can see how good I really am!
  
  Background:
    Given the following projects exist:
      | name       | url                | active |
      | Project 1  | www.project-1.com  | true   |
      | Project 2  | www.project-2.com  | false  |
      | Project 3  | www.project-3.com  | true   |
      | Project 4  | www.project-4.com  | true   |
      | Project 5  | www.project-5.com  | true   |
      | Project 6  | www.project-6.com  | false  |
      | Project 7  | www.project-7.com  | true   |
      | Project 8  | www.project-8.com  | true   |
      | Project 9  | www.project-9.com  | false  |
      | Project 10 | www.project-10.com | false  |
      | Project 11 | www.project-12.com | true   |

  Scenario: View the portfolio
    Given I am on the home page
    Then I should see the following projects
      | name      |
      | Project 1 |
      | Project 3 |
      | Project 4 |
      | Project 5 |
      | Project 7 |
      | Project 8 |

And these steps:

Given /^the following projects exist:$/ do |table|
  # http://railscasts.com/episodes/159-more-on-cucumber uses a FactoryGirl Factory here!
  # more information: http://www.claytonlz.com/index.php/2010/03/zero-to-tested-with-cucumber-and-factory-girl/
  @projects = table.hashes.each { |attributes| Factory(:project, attributes) }
end


Then /^I should see the following projects$/ do |expected_table|
  # This is not a recommended practice – your Then steps should compare against what users see, and they can’t see your database
  expected_table.diff!(@projects.map(&:attributes)) # not that this works anyway, Project.all works...
end

The problem here is two fold. First I want to test this properly (using the view) instead of the database (see comment in last step), I feel that I should know how to do this but I just don't know...

Secondly I will want to write a scope to find only active projects. This will fall in the model so I figure a unit test is required (although the scope is simple enough to probably not require one). Also something that I didn't mention and didn't make it into the feature (where should it go?) is that only 6 projects will be shown on a page.

If anyone can give me some guidance it would be appreciated (ftw I am slowly reading through the RSpec book).

Thanks.

Re: How do I test this?

First I want to test this properly (using the view) instead of the database (see comment in last step), I feel that I should know how to do this but I just don't know...

script/cucumber should have given you a web_steps.rb with predefined steps that give you all you need to navigate your application and verify expectations. For what you're trying to do there is a step

Then I should see "expected text"

Secondly I will want to write a scope to find only active projects. This will fall in the model so I figure a unit test is required (although the scope is simple enough to probably not require one).

You can either extend your feature so it checks that an inactive project is not visible. Or test the scope in a unit test, then check in a controller test that the scope is used.

There are always multiple ways to test the same thing, which is great. You should always choose the path that feels easier.

Also something that I didn't mention and didn't make it into the feature (where should it go?) is that only 6 projects will be shown on a page.

If the listed projects are ordered, you could check if the seventh project isn't visible. I personally don't care much about testing pagination since that is done by a tested plugin (will_paginate) and any problem with the integration of that plugin should manifest themselves in another way (nothing listed at all, or an error is raised).

Re: How do I test this?

You're right, cucumber did give me an example step:

Then /^I should see the following projects$/ do |expected_table|
  # table is a Cucumber::Ast::Table
end

I see what you are saying, I could write it out like:

Then I should see "Project 1"
  And I should see "Project 3"
  And I should see "Project 4"
  And I should see "Project 5"
  And I should see "Project 7"
  And I should see "Project 8"

I just thought writing it out in a table would be a little bit nicer (I guess)... It would also allow me to use the diff! command (as I showed previously) but now doing some further reading it is really meant for tables (in the view)...

Thanks for your input and I think I will do as you suggest.