views:

35

answers:

1

I've got the following spec in spec/views/users/new.html.erb_spec.rb:

require 'spec_helper'

describe "users/new.html.erb" do 
  it "displays the text attribute of the message" do
    render
    response.should contain("Register")
  end 
end

But when I run the test it fails with:

ActionView::TemplateError in 'users/new.html.erb displays the text attribute of the message'
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

The line it is failing on is:

<% form_for @user, :url => account_path do |f| %>

In my Users controller for the new method, I have this:

@user = User.new

Any ideas why I'm getting that error?

UPDATE: Per request, here's my routes file...

ActionController::Routing::Routes.draw do |map|
  map.resource :account, :controller => "users"
  map.resources :users

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end
+1  A: 

The view specification is run in complete isolation from the Users controller. Thus, you have to initialize the variables needed in the view yourself, as described here. The result would be something like this:

require 'spec_helper'
require 'path/to/user.rb'

describe "users/new.html.erb" do 
  it "displays the text attribute of the message" do
    assigns[:user] = User.new
    render
    response.should contain("Register")
  end 
end

If you want to test your view together with your controller, I would suggest looking into integration testing with Cucumber.

gnab