2011-03-04 146 views
1

我在这里跟踪this tutorial,到目前为止所有工作都很成功。简单的RSpec测试失败

但现在,我已经进步到会议上,一些简单的RSpec的测试失败:

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 

当我运行rspec的,我总是得到:

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 '

当我访问会话/新页面,该页面包含如下标题标签:

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

为什么这些测试失败,而所有的oth呃类似(=标题标签的测试)测试工作正常吗?

这里的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 

什么我错在这里做什么?

THX对您有所帮助

回答

5

您需要添加render_views在班上名列前茅。没有它,实际的HTML将不会生成,并且您的测试将失败。

+0

啊,很好,做了伎俩,thx – DeX3 2011-03-04 17:05:11