简体   繁体   中英

Simple RSpec tests failing

I'm following this tutorial here, and everything has worked out very well so far.

But now that I've progressed to sessions, some simple rspec tests are failing:

describe SessionsController do

  #[...]

  describe "GET 'new'" do

    it "should have the right title" do
      get :new
      response.should have_selector( "title", :content => "Sign in" )
    end

  end

  #[...]

  describe "POST 'create'" do

    #[...]

    it "should have the right title" do
      post :create, :session => @attr
      response.should have_selector("title", :content => "Sign in")
    end

    #[...]
  end

end

When I run rspec, I always get:

1) SessionsController GET 'new' should have the right title Failure/Error: response.should have_selector( "title", :content => "Sign in
) expected following output to contain a Sign in tag: w3.org/TR/REC-html40/loose.dtd"> # ./spec/controllers/sessions_controller_spec.rb:14:in `block (3 levels) in '

When I access the sessions/new page, the page contains a title tag like the following:

<title>Ruby on Rails Tutorial Sample App | Sign in</title> 

Why do those tests fail, while all other similar (= tests for the title tag) tests work fine?

Here's the SessionController:

class SessionsController < ApplicationController
  def new
    @title = 'Sign in'
  end

  def create

    user = User.authenticate( params[:session][:email], params[:session][:password] )

    if user.nil?
        flash.now[:error] = "Invalid email/password combination."
        @title = 'Sign in'
        render 'new'
    else
        sign_in user
        redirect_to user
    end
  end


    def destroy
        sign_out
        redirect_to root_path       
    end

end

What am I doing wrong here?

thx for your help

You need to add render_views to the top of the class. Without it, the actual html will not be generated and your have_selector tests will fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM