RSpec with Rails 3.1rc4: spec test won't recognize <%= %> (should be simple)

In my user_sessions_controller:

class UserSessionsController < ApplicationController

before_filter :require_no_user, :only => [:create, :new]

before_filter :require_user, :only => :destroy

def new

@user_session = UserSession.new

@message = “Hello.”

end

def create

@user_session = UserSession.new(params[:user_session])

if @user_session.save

flash[:notice] = “Login successful!”

redirect_back_or_default admin_path

else

render :action => :new

end

end

def destroy

current_user_session.destroy

flash[:notice] = “Logout successful!”

redirect_back_or_default new_user_session_url

end

end

This is not really an rspec issue, but a rails view testing issue. When you render a view directly in a test, it does not use the associated controller, and it is up to you to set up the instance variables needed by the view. In this case:

it "shows the title" do   assign(:message, "Hello.")   render   rendered.should have_content("Admin Login")   rendered.should have_content("Hello.") end

HTH, David

Thank you very much! So I guess it was a simple issue.

But what would I do for the form_for @user_session? I tried…

before(:each) do

assign(:user_session, mock_model(“UserSession”).as_new_record)

end

But that returns the error:

Failure/Error: assign(:user_session, mock_model(“UserSession”).as_new_record)

ArgumentError:

The mock_model method can only accept as its first argument:

  • A String representing a Class that does not exist

  • A String representing a Class that extends ActiveModel::Naming

  • A Class that extends ActiveModel::Naming

It received UserSession

Is this because I’m using Authlogic and the UserSession model extends Authlogic::Session::Base? How would I work around this?

Don't use mock_model :slight_smile: You can use a standard test double:

  assign(:user_session, double("UserSession"))

or a real UserSession object if it's not complicated to set up.

HTH, David

…That’s odd. I just read The RSpec Book, and it explains how double isn’t sufficient when you’re testing something related to form_for. The book, in its example, says to use mock_model(“Message”).as_new_record…

and anyway in my case neither method is working. If I use mock_model, I get

Failure/Error: assign(:user_session, mock_model(“UserSession”).as_new_record)

ArgumentError:

The mock_model method can only accept as its first argument:

  • A String representing a Class that does not exist

  • A String representing a Class that extends ActiveModel::Naming

  • A Class that extends ActiveModel::Naming

It received UserSession

…and if I use double, I get the error the book says I’d get: “undefined method `model_name’ for RSpec::Mocks::Mock:Class”

:confused: so I wonder what the best way to test form_for is now with rspec… btw I’m using Capybara.

Didn't realize you needed it for form_for. You'll need to either use the real object, or stub out all the necessary methods yourself (or submit a patch to Authlogic to get it to conform to ActiveModel's API).

Cheers, David

Thanks again - I just used the model directly.