views:

62

answers:

1

Rails3 app with Rspec2 and Cucumber

Cucumber

Given /^that a user is logged in$/ do
  current_user = User.first
  render new_user_post_path(current_user)
end

Routes.rb

map.resources :users do |users|
  users.resources :posts, :collection => {:view => :get}
end

posts_controller_spec

describe PostsController do
  describe "#new" do
    it "should be successful" do
      get :new
      response.should be_success
    end
  end
end

My first big epic Fail

(::) failed steps (::)

No route matches {:controller=>"posts", :action=>"new"} (ActionController::RoutingError)
./features/step_definitions/tasklist_steps.rb:3:in `/^that a user is logged in$/'
features/tasklist.feature:7:in `Given that a user is logged in'

Failing Scenarios:
cucumber features/tasklist.feature:6 # Scenario: List SubmitLink

1 scenario (1 failed)
3 steps (1 failed, 2 skipped)
0m0.147s
rake aborted!

Sorry, I'm too newb. This is my first ever attempt at cucumber. :(

+2  A: 

Well first off map is deprecated in Rails 3 routes, you should probably have something like this.

resources :users do 
    resources :posts
end

And They way I normally write my givens is like this: This way I am following the path that the user would actually take, by creating the user in the test db, and then actually going to the login page and signing in, so everything gets sets correctly in the app. Are you using a premade authentication solution, like devise or authlogic` ?

Given /^I have one\s+user "([^\"]*)" with email "([^\"]*)" and password "([^\"]*)"$/ do |username,email, password|
  @user = User.new(:email => email,
                   :username=>username,
                   :password => password,
                   :password_confirmation => password)
   @user.save!
end

Given /^I am an authenticated user$/ do
  name = 'exmample'
  email = '[email protected]'
  password = 'secret!'

  Given %{I have one user "#{name}" with email "#{email}" and password "#{password}"}
  And %{I go to the user login page}
  And %{I fill in "user_username" with "#{name}"}
  And %{I fill in "user_password" with "#{password}"}
  And %{I press "Sign in"}
end

And I use it the following way:

Feature: 
  In order to reboot my crappy/POS equipment without bothering the awesome Noc Monkeys
  I would like to login to a webpage and see the status of and control the outlets on my equipment

  Background: Valid and authenticated user with at least one outlet to control
    Given I am an authenticated user

  @ok
  Scenario: Viewing All outlets
    Given I am able to control an outlet with index "1"
    And I am on the home page
    Then I should see "server_1"

Also normally I don't call render inside a cucumber step. Since you are working with a simulated browser (assuming webrat/capybara) You would visit path_to(page_name).

Doon
Righteo. I figured it out. Thanks a bazillion. I'm sure I'll fall in a dozen more pitfalls between now and being an even lesser newb ;) . Thanks for the help.
Trip