目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -4
访问者希望看到用户的信息
git checkout -b f5
rails c # console
rails s # server
spec/features/guest_can_see_user_info_spec.rb
# coding: utf-8
feature '访问者希望看到用户的信息' do
scenario '访问/users/:id, 应该显示用户信息' do
visit "/users/1"
page.should have_content "test_user"
end
end
No route matches [GET] "/users/1"
/users/1
对应的路由信息config/routes.rb
,增加路由信息resources :users, only: [:show]
uninitialized constant UsersController
UsersController
rails console
中执行[7] pry(main)> generate "controller users"
create app/controllers/users_controller.rb
invoke haml
create app/views/users
=> "Completed"
[8] pry(main)>
The action 'show' could not be found for UsersController
show
方法controllers/users_controller
,增加show
方法class UsersController < ApplicationController
def show
end
end
Missing template users/show, application/show with {:loc...
show
模板users/show.html.haml
拷贝ui/user.html
内容至当前模板
%section#user.box
%h1 个人信息
%img(src="#{gravatar_url('[email protected]')}")
%dl
%dt.span1 ID:
%dd
%strong chucai
%dt.span1 Since:
%dd 2012年2月12日
注意:当前用户故事只考虑基本用户信息,最新帖子和最新回复以后的步骤中会增加
expected there to be text "test_user" in "社区 会员 knowa...
users/show.html.haml
%section#user.box
%h1 个人信息
%img(src="#{gravatar_url('[email protected]')}")
%dl
%dt.span1 ID:
%dd
%strong= @user.name
%dt.span1 Since:
%dd= @user.created_at
undefined methodname' for nil:NilClass
`@user
变量赋值controllers/users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
end
uninitialized constant UsersController::User
user
这个model
rails console
中执行[8] pry(main)> generate "model user name:string"
invoke active_record
create db/migrate/20130101221838_create_users.rb
create app/models/user.rb
=> "Completed"
[9] pry(main)>
bundle exec rake db:migrate
bundle exec rake db:test:prepare
Couldn't find User with id=1
user
部分# coding: utf-8
feature '访问者希望看到用户的信息' do
background do
@user = User.create name: "test_user"
end
scenario '访问/users/:id, 应该显示用户信息' do
visit "/users/#{@user.id}"
page.should have_content @user.name
page.should have_content @user.created_at.to_s
end
end
git add .
git commit
git checkout dev
git merge f5 --no-ff
git branch -d f5