views:

110

answers:

1

Hi there,

i am trying to write a view_spec for my login form that uses authlogic. This is my current view_spec:

require 'spec_helper'

describe "user_sessions/new.html.haml" do
  before :each do
    user_session = mock("UserSession").as_null_object
    assign(:user_session, user_session)
  end

  it "should have a login form" do
    render 
    rendered.should have_selector("form", :id => "new_user_session",:action => user_sessions_path, :method => "post")
  end
end

this is my view

%h1 Login/Registrierung
= form_for @user_session do |f|
  .text_field
    = f.label :email
    = f.text_field :email

  .password_field
    = f.label :password
    = f.password_field :password

  .submit
    = f.submit "Einloggen oder Account erstellen"

It fails with this error:

Finished in 0.01737 seconds
1 example, 1 failure

1) user_sessions/new.html.haml should have a login form
    Failure/Error: render
    undefined method `model_name' for RSpec::Mocks::Mock:Class

Usually this means I have to use mock_model("UserSession").as_new_record. But UserSession is not ActiveModel - and mock("UserSession").as_new_record does not work and fails with Mock "UserSession" received unexpected message :as_new_record with (no args).

How to deal with that? I'm sure others successfully wrote view_specs for login forms with Authlogic.

A: 

See http://stackoverflow.com/questions/1025594/how-can-i-use-mock-models-in-authlogic-controller-specs , although I dont quite see why you need to mock the UserSession. Why not just use the real thing and fixtures / factory_girl_rails ?

runeb