目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -6
访问者希望登录
git checkout -b f7
rails c # console
rails s # server
spec/features/guest_can_sign_in_spec.rb
# coding: utf-8
feature '访问者希望登录' do
background do
@user = User.create name: "test_user", email: "[email protected]"
end
scenario '输入用户名后点击"登录"按钮,应该正常登录' do
visit "/sign_in"
fill_in "user_name", with: @user.name
click_button "登录"
current_path.should == root_path
end
end
No route matches [GET] "/sign_in"
sign_in
的路由信息config/routes.rb
,增加路由信息match 'sign_in', to: "sessions#new"
uninitialized constant SessionsController
sessions
这个controller
rails console
中执行[10] pry(main)> generate "controller sessions"
create app/controllers/sessions_controller.rb
invoke haml
create app/views/sessions
=> "Completed"
[11] pry(main)>
The action 'new' could not be found for SessionsController
new
方法controllers/sessions_controller.rb
,增加new
方法class SessionsController < ApplicationController
def new
end
end
Missing template sessions/new, application/new with {:lo...
new
模板sessions/new.html.haml
模板拷贝ui/signin.html
内容至当前模板
= render 'shared/breadcrumb', links: [["Home", ""]]
%section#sign_in.box
%h1 登录
%form(action="" method="post" class="form-horizontal")
.control-group
%label(class="control-label") 用户名
.controls
%input(type="text")
.submit-button
%input(type="submit" value="登录" class="btn btn-primary")
%section#sidebar
%section#has_account.box
%h2 还没有帐号?
= link_to "注册"
Unable to find field "user_name"
sessions/new.html.haml
模板= render 'shared/breadcrumb', links: [["Home", ""]]
%section#sign_in.box
%h1 登录
= form_tag sessions_path, method: :post, class: "form-horizontal" do
.control-group
= label_tag :user_name, "用户名", class: "control-label"
.controls
= text_field_tag :user_name
.submit-button
= submit_tag "登录", class: "btn btn-primary"
%section#sidebar
%section#has_account.box
%h2 还没有帐号?
= link_to "注册"
undefined local variable or methodsessions_path' for #<...
`sessions#create
的路由信息config/routes.rb
,增加路由信息resources :sessions, only: [:create]
The action 'create' could not be found for SessionsController
create
方法controllers/sessions_controller
,增加create
方法def create
end
Missing template sessions/create, application/create with {:l...
sessions/create
模板controllers/sessions_controller
,修改create
方法内容def create
session[:user_name] = params[:user_name]
redirect_to root_path
end
spec/features/guest_can_sign_in_spec.rb
scenario '应该显示用户名称' do
visit "/sign_in"
fill_in "user_name", with: @user.name
click_button "登录"
page.should have_content @user.name
end
expected there to be text "test_user" in "社区 会员 knowa...
layouts/application.html.haml
把第 22 行 knowang 的部分修改为实际代码
= current_user.name
undefined local variable or methodcurrent_user' for #<...
`current_user
方法controllers/application_controller.rb
,增加current_user
的定义class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
private
def current_user
user = User.new
user = User.find_by_name(session[:user_name]) if session[:user_name]
user
end
end
git add .
git commit
git checkout dev
git merge f7 --no-ff
git branch -d f7