测试 使用 RSpec+Capybara 简单 BDD 入门 -6

blueplanet · January 05, 2013 · 3836 hits

目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -5

用户故事

访问者希望注册用户

  • 通过用户名和 Email 注册

环境准备

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

下一步骤:使用 RSpec+Capybara 简单 BDD 入门 -7

blueplanet in 使用 RSpec+Capybara 简单 BDD 入门 -5 mention this topic. 03 Apr 10:56
blueplanet in 使用 RSpec+Capybara 简单 BDD 入门 -7 mention this topic. 03 Apr 10:56
You need to Sign in before reply, if you don't have an account, please Sign up first.