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 738the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8051. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8771. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8351.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7021. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8561. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6481. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10631. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 687in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 731in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 603again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6611. git checkout -b updating-use ... -
9.3 sign in success.
2011-10-12 15:39 6811. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 670There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 605start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 612a 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 675in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 732Chapter 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...
EhLib 9.4 Build 9.4.012源码版,值得拥有,可以安装,已测试
postgresql 9.4 版本连接jdbc驱动包
EhLib 9.4.022 FOR DELPHI
Altium Designer 9.4 破解 注意是最新版本9.4的呀!!! 内有详细说明! 亲测! 仅供测试用,请试用后删除,祝成功!
jetty 9.4.9, jetty 容器是轻便型容器,启动速度的确可以 ,附带有servlet-api.jar 和jsp-api.jar 两个jar包
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 ...
openssh-9.4p1的rpm包
Base SAS® 9.4 Procedures Guide
bartender9.4-crack.zip 破解版标签打印工具
EhLib.VCL 9.4 Build 9.4.022 Professional Edition
Pentaho Kettle 9.4
openssh-9.4p1
mingw-9.4.exe,不需要在线下载 直接安装就可以
FANUC机器人仿真软件robogiodeV9.4+补丁,亲测可用
EhLib 9.4 Build 9.4.009 Professional Edition(源码版),支持Borland Delphi versions 7, 9, Developer Studio 2006, Delphi 2007, Embarcadero RAD Studio 2009-XE10.3, Lazarus.
centos7 openssh9.4
FSCapturev9.4软件,可以用来截屏,包括长截屏、固定区域截屏,还有录屏功能,还可以取色,特别适合前端开发者,小巧好用,windows下可用
IBM informix Dynamic Server 入门指南v9.4