简单的RSpec测试失败

时间:2011-03-04 16:34:40

标签: ruby-on-rails ruby-on-rails-3 rspec rspec2

我在这里关注this tutorial,到目前为止一切都很顺利。

但是现在我已经进入了会话,一些简单的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'应该   拥有正确的头衔        失败/错误:response.should have_selector(“title”,:content =>   “登录
  )          预计在输出后包含登录标记:             w3.org/TR/REC-html40/loose.dtd">        #./spec/controllers/sessions_controller_spec.rb:14:in   '阻止(3级)'

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

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

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

这是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求助

1 个答案:

答案 0 :(得分:5)

您需要将render_views添加到班级的顶部。没有它,将不会生成实际的html,并且您的have_selector测试将失败。