目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -5
访问者希望注册用户
git checkout -b f6
rails c # console
rails s # server
spec/features/guest_can_sign_up_spec.rb
# coding: utf-8
feature '访问者希望注册用户' do
scenario '访问/sign_up, 应该显示注册用页面' do
visit '/sign_up'
page.should have_field "user_name"
page.should have_field "user_email"
end
end
No route matches [GET] "/sign_up"
sign_up
的路由信息config/routes.rb
,增加下列路由match 'sign_up', to: "users#new"
The action 'new' could not be found for UsersController
users_controller
没有new
方法controllers/users_controlelr.rb
,增加new
方法class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
end
end
Missing template users/new, application/new with {:locale=>[:e...
new
模板users/new.html.haml
模板拷贝ui/signup.html.haml
内容至当前模板
expected to find field "name" but there were no matches
users/new.html.haml
模板= render 'shared/breadcrumb', links: [["Home", ""]]
%section#new_user.box
%h1 注册新用户
= form_for @user, html: {class: "form-horizontal"} do |f|
.control-group
= f.label "用户名", class: "control-label"
.controls
= f.text_field :name
= f.label "Email", class: "control-label"
.controls
= f.text_field :email, placeholder: "[email protected]"
.submit-button
= f.submit "提交注册信息", class: "btn btn-primary"
%section#sidebar
%section#has_account.box
%h2 已经有帐号了?
= link_to "登录"
undefined methodmodel_name' for NilClass:Class
`@user
变量controllers/users_controller.rb
,在new
方法中增加对@user
赋值def new
@user = User.new
end
undefined methodusers_path' for #<#<Class:0x007fd32d...
`users#create
的路由信息config/routes.rb
,增加users#create
的路由信息resources :users, only: [:show, :create]
undefined methodemail' for #<User id: nil, nam...
`user
中没有定义email
字段rails consle
中执行[9] pry(main)> generate "migration AddEmailToUsers email:string"
invoke active_record
create db/migrate/20130102003510_add_email_to_users.rb
=> "Completed"
[10] pry(main)>
bundle exec rake db:migrate
bundle exec rake db:test:prepare
###增加测试用例
# coding: utf-8
feature '访问者希望注册用户' do
scenario '访问/sign_up, 应该显示注册用页面' do
visit '/sign_up'
page.should have_field "user_name"
page.should have_field "user_email"
end
scenario '输入用户名和Email,点击"提交注册信息"按钮,应该显示首页' do
visit '/sign_up'
fill_in "user_name", with: "test_user"
fill_in "user_email", with: "[email protected]"
click_button "提交注册信息"
current_path.should == root_path
end
end
The action 'create' could not be found for UsersController
create
方法controllers/users_controller.rb
,增加create
方法def create
end
Missing template users/create, application/create with {:loca...
create
模板create
方法def create
User.create(params[:user])
redirect_to root_path
end
Can't mass-assign protected attributes: email
email
的属性声明model/user.rb
,增加email
属性声明attr_accessible :name, :email
git add .
git commit
git checkout dev
git merge f6 --no-ff
git branch -d f6