On Jun 29, 11:07 am, David Zhang <dzhan...@gmail.com> wrote:
> 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
>
> ========
> In views/user_sessions/new.html.erb:
>
> <!--user_sessions#new-->
> Admin Login
> <%= @message %>
>
> ========
> In spec/views/user_sessions/new.html.erb_spec.rb:
>
> require 'spec_helper'
>
> describe "user_sessions/new.html.erb" do
>
>   context "displays the admin login form" do
>     it "shows the title" do
>       render
>       rendered.should have_content("Admin Login") # this is fine
>       rendered.should have_content("Hello.") # I added this just to test
> something simple, but it didn't work
>     end
>
>     it "shows the form"
>
>     it "shows the button"
>   end
>
> end
> ========
> If I do "rails server" and check out the page in the browser, there is the
> "Hello." just as I expect.  But the spec test fails to get it.. it /should/
> pass:
> 1) user_sessions/new.html.erb displays the admin login form shows the title
>      Failure/Error: rendered.should have_content("Hello.")
>        expected there to be content "Hello." in "Admin Login\n\n\n\n"
>      # ./spec/views/user_sessions/new.html.erb_spec.rb:9:in `block (3
> levels) in <top (required)>'
>
> So why might the <%= %> not be properly showing the @message?
>
> I actually originally had the form code in the view:
> <%= form_for @user_session, :url => user_session_path do |f| %>
> <%= f.label :login %>
> <%= f.text_field :login, :id=>"admin_login" %>
>
> <%= f.label :password %>
> <%= f.password_field :password, :id=>"admin_password" %>
>
> <%= f.submit "Login", :id=>"admin_login_button" %>
> <% end %>
>
> .. but testing for this yielded
> Failure/Error: render
>      ActionView::Template::Error:
>        undefined method `model_name' for NilClass:Class
>
> So the @user_session variable wasn't being recognized by the spec test.
>  Similar problem?
>
> Assistance is appreciated.

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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to