`
peryt
  • 浏览: 55269 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

9.4 sign out

 
阅读更多

whew!!!, last chapter is a long one!

 

now, we are going to implement sign out.

 

1. no wonders, we will start from TDD.

 

 

describe "DELETE 'destroy'" do
	it "should sign a user out" do
		test_sign_in(Factory(:user))
		delete :destroy
		controller.should_not be_signed_in
		response.should redirect_to(root_path)
	end
end
 

now we define the test_sign_in method in the spec_helper.

 

RSpec.configure do |config|
  def test_sign_in(user)
    controller.sign_in(user) 
  end
end

 2. the destroy function in controller:

 

 

def destroy
  sign_out
  redirect_to root_path
end

 

3. the sign_out fuction in sessions_helper.rb

 

 

def sign_out
  cookies.delete(:remember_token)
  self.current_user = nil
end

 

4. sign in upon sign up.

 

let's first write a test to test that a signup user is auto signed in.

 

 

describe Usercontroller do
	describe "POST 'create'" do
		it "should sign the user in" do
			post :create, :user => @attr
			controller.should be_signed_in
		end
	end
end

 then we just need to add a line of code to the User models create method:

 

if @user.save
	sign_in @user
	flash[:success] = "Welcom....."
	redirect_to @user
else
	.....
end

 

5. next, we will change the layout links, so that after user signed in, the link text change to "sign out"

although what we want to change is views, we can still strat from TDD of integration test.

 

luckily, we already have Layout links integration test.

 

describe "Layout links" do
	describe "when not signed in" do
		it "should have a sign in link" do
			visit root_path
			response.should have_selector("a", :href => signin_path, :content => "Sign in")
		end
	end
	describe "when signed in" do
		before(:each) do
			@user = Factory(:user)
			visit signin_path
			fill_in :email, :with => @user.email
			.....
			click_button
		end
		it "should have a sign out link" do
			visit root_path
			reponse.should have_selector("a", :href => signout_path, :content => "Sign out")
		end
		it "should have a profile link" do
                    visit root_path
                    response.should have_selector("a", :href => user_path(@user), :content => "Profile")
                 end
end

 now it is time the change the layout header, to change the link according to signed in or not.

 

 

<% if signed_in? %>
<li><%= link_to "Sign out", signout_path, :method => :delete %></li>
<%else%>
<li><%= link_to "Sign in", signin_path%></li>
<%end%>

 

for the view, need to add the profile link:

link_to "Profile", current_user

 

you can see that rails allow to specify a object to stand for the url

 

 

6. now we can add a integration test for both sign in and sign out.

I'll put this test in to users_spec.rb integration test.

 

 

describe "sign in/out" do
	describe "failure" do
		it "should not sign a user in" do
			visit signin_path
			fill_in :email, :with =>""
			fill_in :password, :with => ""
			click_button
			response.should have_selector("div.flash.error", :content => "Invalid")
		end
	end
	describe "success" do
		it "should sign user in and out" do
			user = Factory(:user)
			visit signin_path
			fill_in :email, :wtih => user.email
			fill_in :password, :with => user.password
			click_button
			controller.should be_signed_in
			click_link "Sign out"
			controller.should_not be_signed_in
		end
	end
end

 

please take special attention to the code:

 

click_link "Sign out"

by this singly line of code, it actuall tested the route, the link text, the url, and the changing of the layout link, 

 

I like this test method very much!!

click_link "Sign out"

分享到:
评论

相关推荐

    Kettle9.4版本 Pentaho Data Integration 9.4 PDI 9.4

    Kettle9.4版本 Pentaho Data Integration 9.4 PDI 9.4 下载地址: https://www.hitachivantara.com/en-us/products/pentaho-platform/data-integration-analytics/pentaho-community-edition.html ...

    openssh最新rpm包openssh-9.4p1openssh9.4p1

    建议升级9.3p2或者9.4p1 openssh-9.4p1-1.el7.x86_64.rpm openssh-askpass-9.4p1-1.el7.x86_64.rpm openssh-askpass-gnome-9.4p1-1.el7.x86_64.rpm openssh-clients-9.4p1-1.el7.x86_64.rpm openssh-debuginfo-9.4p1...

    postgresql 9.4 jdbc 包

    postgresql 9.4 版本连接jdbc驱动包

    EhLib 9.4 Build 9.4.012.rar

    EhLib 9.4 Build 9.4.012 是一个专为数据库应用程序开发设计的组件库,尤其在.NET Framework环境下具有广泛的应用。这个版本的EhLib提供了丰富的功能,包括数据访问、数据库连接、数据绑定以及数据处理等,帮助...

    Pentaho Kettle 9.4

    Pentaho Kettle 9.4

    jetty-client-9.4.43.v20210629-API文档-中文版.zip

    赠送jar包:jetty-client-9.4.43.v20210629.jar; 赠送原API文档:jetty-client-9.4.43.v20210629-javadoc.jar; 赠送源代码:jetty-client-9.4.43.v20210629-sources.jar; 赠送Maven依赖信息文件:jetty-client-...

    jetty 9.4.9

    Jetty 9.4.9 是一个开源的Java Web服务器和Servlet容器,以其轻量级、高效和灵活性而受到开发者的欢迎。这个版本是Jetty项目的一个重要里程碑,提供了许多性能改进和新特性。在深入探讨之前,让我们先了解一些基本...

    Memtest86 Pro 9.4

    **Memtest86 Pro 9.4:内存测试的专业选择** Memtest86 Pro 9.4是一款专门针对计算机内存进行深度检测和故障排查的工具。在计算机硬件中,内存扮演着至关重要的角色,它是系统运行速度和稳定性的重要决定因素。然而...

    AD9.4破解补丁

    AltiumDesignerSummer9Build9.4.0.20159破解补丁

    Altium Designer 9.4注册

    9.4版本是其历史上的一个重要版本,提供了丰富的功能和改进,以帮助工程师们更高效地完成设计任务。 在电子设计领域,Altium Designer拥有以下几个核心知识点: 1. **原理图设计**:Altium Designer提供了直观的...

    bartender9.4.zip

    bartender9.4破解版,内涵注册机。bartender是最好的标签打印软件。此为非开发版本。可满足日常打印需求。

    haFSCapturev9.4取色软件.rar

    "haFSCapturev9.4取色软件.rar" 是一个压缩文件,其中包含了名为 "haFSCapturev9.4.exe" 的可执行程序,该程序是FSCapture软件的v9.4版本。FSCapture是一款高效且功能丰富的屏幕截图工具,特别针对取色功能进行了...

    EhLib 9.4.022

    EhLib 9.4.022 FOR DELPHI

    Altium Designer 9.4破解

    Altium Designer 9.4 破解 注意是最新版本9.4的呀!!! 内有详细说明! 亲测! 仅供测试用,请试用后删除,祝成功!

    EhLib 9.4 Build 9.4.015 Professional Edition with Full Source.rar

    EhLib 9.4源码,亲测可用,  New type of data display in TPlannerEh - annual period broken down by days.  In DBGridEh added the ability to save filters through the SettingsKeeper specified in the ...

    EhLib 9.4 Build 9.4.12 Professional Edition(源码版)

    EhLib 9.4 Build 9.4.12 Professional Edition(源码版),支持Borland Delphi versions 7, 9, Developer Studio 2006, Delphi 2007, Embarcadero RAD Studio 2009-XE10.3.2, Lazarus. 【Version 9.4】 + New type of ...

    servu9.4 绿化 版

    【标题】"ServU 9.4 绿化版"是指ServU的9.4版本经过绿化处理后的软件,绿化版通常是指将原版软件进行优化,去除安装过程,简化为解压即用的形式,方便用户快速部署和使用。ServU是一款著名的FTP(File Transfer ...

    bartender9.4-crack.zip

    bartender9.4-crack.zip 破解版标签打印工具

    openssh-9.4p1的rpm包

    openssh-9.4p1的RPM包是OpenSSH软件在Linux系统中的一个特定版本。OpenSSH(Open Source Secure SHell)是一个提供安全网络服务的开源项目,它包括了SSH客户端和服务器组件,用于在不安全的网络环境中提供加密的远程...

    sas9.4安装教程.7z

    《SAS 9.4 安装教程详解》 SAS(Statistical Analysis System)是一款强大的统计分析软件,被广泛应用于科研、教育、商业等多个领域。SAS 9.4是其最新版本,提供了更丰富的功能和优化的性能。本文将深入探讨SAS 9.4...

Global site tag (gtag.js) - Google Analytics