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"
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 825the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8611. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9141. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8801.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7551. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 9011. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7051. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11081. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 746in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 776in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 668again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7101. git checkout -b updating-use ... -
9.3 sign in success.
2011-10-12 15:39 7511. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 727There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 660start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 645a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 723in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 782Chapter 8.3 this part, we will ...
相关推荐
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 ...
建议升级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驱动包
EhLib 9.4 Build 9.4.012 是一个专为数据库应用程序开发设计的组件库,尤其在.NET Framework环境下具有广泛的应用。这个版本的EhLib提供了丰富的功能,包括数据访问、数据库连接、数据绑定以及数据处理等,帮助...
Pentaho Kettle 9.4
赠送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 是一个开源的Java Web服务器和Servlet容器,以其轻量级、高效和灵活性而受到开发者的欢迎。这个版本是Jetty项目的一个重要里程碑,提供了许多性能改进和新特性。在深入探讨之前,让我们先了解一些基本...
**Memtest86 Pro 9.4:内存测试的专业选择** Memtest86 Pro 9.4是一款专门针对计算机内存进行深度检测和故障排查的工具。在计算机硬件中,内存扮演着至关重要的角色,它是系统运行速度和稳定性的重要决定因素。然而...
AltiumDesignerSummer9Build9.4.0.20159破解补丁
9.4版本是其历史上的一个重要版本,提供了丰富的功能和改进,以帮助工程师们更高效地完成设计任务。 在电子设计领域,Altium Designer拥有以下几个核心知识点: 1. **原理图设计**:Altium Designer提供了直观的...
bartender9.4破解版,内涵注册机。bartender是最好的标签打印软件。此为非开发版本。可满足日常打印需求。
"haFSCapturev9.4取色软件.rar" 是一个压缩文件,其中包含了名为 "haFSCapturev9.4.exe" 的可执行程序,该程序是FSCapture软件的v9.4版本。FSCapture是一款高效且功能丰富的屏幕截图工具,特别针对取色功能进行了...
EhLib 9.4.022 FOR DELPHI
Altium Designer 9.4 破解 注意是最新版本9.4的呀!!! 内有详细说明! 亲测! 仅供测试用,请试用后删除,祝成功!
bartender9.4-crack.zip 破解版标签打印工具
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(源码版),支持Borland Delphi versions 7, 9, Developer Studio 2006, Delphi 2007, Embarcadero RAD Studio 2009-XE10.3.2, Lazarus. 【Version 9.4】 + New type of ...
【标题】"ServU 9.4 绿化版"是指ServU的9.4版本经过绿化处理后的软件,绿化版通常是指将原版软件进行优化,去除安装过程,简化为解压即用的形式,方便用户快速部署和使用。ServU是一款著名的FTP(File Transfer ...
openssh-9.4p1的RPM包是OpenSSH软件在Linux系统中的一个特定版本。OpenSSH(Open Source Secure SHell)是一个提供安全网络服务的开源项目,它包括了SSH客户端和服务器组件,用于在不安全的网络环境中提供加密的远程...
《BarTender 9.4:专业条码与标签设计软件深度解析》 在信息化管理日益普及的时代,条码和二维码已经成为数据交换的关键媒介。BarTender 9.4,这款由Seagull Scientific公司推出的强大条码打印软件,以其专业性和...