Chapter 7.3
now, we have a good user model, we can try to make a show page to show the user info.
we already have some test for the new action in the users controller.
now we will add some test for the show action in this controller.
But at once, we facing a challenge, the test of show action will need to the use of an instance of the User model. We will overcome it using the technique of
"factories"
(many experienced Rails programmers find factory is much more flexible and easir to maintain then fixtures, which Rails uses by default)
we will use the factories generated by Factory Girl, a ruby gem.
so we need to add this gem to gem file,
(since it is only used in test, so need to include it into the :test group)
group :test do
gem 'factory_girl_rails', '1.0'
end
then run
bundle install
next, we are ready to create the file
spec/factories.rb
in this file, it will define a User factory, since this file is under spec dir, so Rspec will load our factories auto whenever test run.
Factory.define :user do |user|
user.name "abcd efg"
user.email "abcd@abcd.com"
user.password "secret"
user.password_confirmation "secret"
end
then in the test, we can create a User factory like this:
@user = Factory(:user)
this line should be in before(:each) block in the spec test file.
2. in functional test, there are four hashs that are ready to use after finishing a browser request:
Assigns - Any objects that are stored as instance variables in actions for use in views.
cookies
flash
session
and, you also have access to 3 instance variables in your functional test:
@controller
@request
@response
3. the users_controller_spec.rb
require 'spec_helper' describe UsersController do render_views describe "GET 'show'" do before(:each) do @user = Factory(:user) end it "should be successful" do get :show, :id => @user response.should be_success end it "should find the right user" do get :show, :id => @user assigns(:user).should == @user end end . . . end
now, you see the use of
assigns(:user)
will return the value of the instance variable @user.
you may note I use
get :show
instead of
get 'show'
because former is more REST!!
you may also note I use
:id => @user
instead of
:id => @user.id
because rails will do the job of converting @user to @user.id, and it is very very common rails way to just use :id => @user
3. ok, we done the test, now we will do the views.
let's start from test again:
it "should have the right title" do get :show, :id => @user response.should have_selector("title", :content => @user.name) end it "should include the user name" do get :show, :id => @user response.should have_selector("h1", :content => @user.name) end it "should have a profile image" do get :show, :id => @user response.should have_selector("h1>img", :class => "gravatar") end
note:
have_selector is a helper method from rspec to test the content inside a html tag.
and it can also test the CSS class of the element.
(the helper method define in application_helper.rb can only be seen by the views!!!)
some malicious user will enter name with script, which would be inject into our application by the title helper, before rails 3, we use h() to escape, (short for html_escape), but in Rails 3, all embedded Ruby text is escaped by default.
so <script> will be embedded as
<script>
(but if you want to not escape, you can use (raw) method.)
(if your app need to handle images or other file uploads, Paperclip gem is the way to go)
4. to deal with img, we will use Gravatar gem.
gem 'gravatar_image_tag', '1.0.0.pre2'
bundle install
<%= gravatar_image_tag 'example@railstutorial.org' %>
user = User.first
user.update_attributes(:email => "jkdlj@jfdl.com", :password => "fo", :password_confirmation => "fo")
we still need to add a :class attr to the img element,
<%= gravatar_image_tag @user.email, :class => "gravatar" %>
but we will use this logo in many places, if we use :class everywhere, it is a dup, so we will extract it into helper.
since this image is in general associated with a user, so we put it into user_helper.rb
(actually, all helper method are viewable in all views, put it to which helper file is just to be more clear for you, doesn't matter at all)
we define a helper method for view to use:
module UserHelper
def gravatar_for(user, options => {:size => 50})
gravatar_image_tag(user.email.downcase, :alt => user.name, :class => 'gravatar', :gravatar => options)
end
end
5. a user sidebar:
<%= link_to user_path(@user), @user%>
you may find why it use @user instead of user_path(@user)
this is also a rails convention, it is the same to
<%= link_to user_path(@user), user_path(@user)%>
but rails like using the former way!!!
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 788the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8421. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8961. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8581.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7291. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8821. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6831. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10871. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 715in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 757in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 634again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6931. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 718whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7211. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 703There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 643start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 637a 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 699in integration test, you can te ...
相关推荐
《CrystalBall用户手册7.3》是一份详细指导用户如何使用这款强大工具的重要参考资料,尤其在蒙特卡洛模拟方面有着显著的应用。CrystalBall是一款基于Excel的预测与决策分析软件,广泛应用于金融、工程、运营管理和...
SecureCRT 7.3&SecureFX 7.3 2015-10-26
secureCRT7.3 secureFX7.3 含注册机 亲测可用 含注册步骤
泰格7.3 泰格7.3 注册机 文件泰格7.3 注册机 文件
Vim(Vi IMproved)是Bill Joy在1980年代为UNIX系统开发的文本编辑器Vi的一个增强版本,而7.3是Vim的重要版本之一,发布于2010年。Vim 7.3引入了许多改进和新特性,使其在功能性和用户体验上都有了显著提升。GVim是...
wincc7.3 crack 破解 ,,工程实践,,本人购买,,
RedHat7.3百度云下载链接
首先,你需要下载SecureCRT 7.3的安装程序,这个压缩包中包含两个版本的安装文件,分别是scrt73-x64.exe(适用于64位操作系统)和scrt73-x86.exe(适用于32位操作系统)。确保根据你的操作系统类型选择正确的安装包...
erwin7.3注册码,使用本注册码注册就可以用了
wincc V7.3免狗破解
wincc7.3 免狗 授权 安装wincc7.3后 先进行替换 然后再进行授权 运行软件 适合WINCC7.3.0.0
CGTECH Vericut7.3 破解文件及安装说明
PDC在2010年5月发表了Visual Prolog 7.3版本。比较7.2和7.3两个版本语言参考手册,新版本主要 的变化是增加了三章内容:通用接口和类、监控程序、属性,当然还有一些其它内容更改。这个译本就是 在译者上一个译本...
《ewebeditor 7.3:一款支持上传的静态版编辑器详解》 ewebeditor 7.3 是一个强大的富文本编辑器,专为网页内容编辑设计,它以其易于使用和丰富的功能集赢得了广大用户的青睐。这个版本的显著特点是支持上传功能,...
Vim 7.3是这个历史悠久的编辑器的一个重要版本,它在前任版本的基础上进行了诸多改进和增强,使得用户体验和工作效率得到了显著提升。 1. **Vim的基本概念与操作**:Vim分为命令模式、插入模式和底线命令模式。在...
《H3C iNode PC 7.3 (E0538) 软件详解》 H3C公司的iNode是一款专为网络设备管理设计的客户端软件,它提供了全面的网络管理和监控功能,帮助用户高效地管理和维护网络环境。在最新的版本iNode 7.3 (E0538 Chinese)中...
《深入理解H3C iNode 7.3 Linux系统》 在信息技术领域,H3C iNode是一款由H3C公司推出的嵌入式Linux操作系统,主要用于网络设备和服务器平台。iNode 7.3作为其重要的版本,为用户提供了一套稳定、高效的运行环境。...
《Mac iNode 7.3 H3C_iNode_PC_7.3_E0517:为Mac用户解决网络接入难题》 Mac iNode 7.3是一款专为苹果Mac OS系统设计的网络接入工具,由H3C公司提供,主要用于帮助用户在特定环境下(如企业或学校)实现网络连接。...
SecureCRT7.3绿色破解免安装版,解压后 直接运行目录中的SecureCRT_keygen.exe,会生成注册用的信息。 再运行SecureCRT.exe,填写刚刚生成的注册信息。 ========== 就能完成注册。 功能描述 终端仿真器 SecureCRT,...