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

blueplanet · January 05, 2013 · 4078 hits

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

用户故事

访问者希望看到帖子的详细信息

  • 显示标题、内容、节点、创建日期
  • 顶部的导航链接

环境准备

git checkout -b f4
rails c # console
rails s # server

步骤

新建文件spec/features/guest_can_see_topic_info_spec.rb

# coding: utf-8
feature '访问者希望看到帖子的详细信息' do
  background do
    @node = Node.create name: "Ruby"
    @topic = Topic.create title: "topic 1 test", node: @node
  end

  scenario '访问/topics/1, 应该显示帖子的详细信息' do
    visit "/topics/#{@topic.id}"

    page.should have_content @topic.title
    page.should have_content @topic.node.name
  end
end
  • 测试失败:No route matches [GET] "/topics/1"
  • 原因:没有topics/1对应的路由信息

编辑config/routes.rb,增加topics show的路由设置

resources :topics, only: [:index, :show]
  • 测试失败:The action 'show' could not be found for TopicsController
  • 原因:TopicsController没有定义show方法

编辑controllers/topics_controller.rb,增加show方法定义

def show
end
  • 测试失败:Missing template topics/show, application/show with {:lo...
  • 原因:没有show模板

增加topics/show.html.haml,拷贝ui/topic.html.haml的内容至当前模板

  • 测试失败:expected there to be text "topic 1 test" in "社区 会...
  • 原因:模板中是写死的假数据

修改topics/show.html.haml内容

= render 'shared/breadcrumb', links: [["Home", ""], ["社区", ""], ["新手问题", ""], ["浏览帖子", ""]]

%section#show_topic
  %section#topic_banner.box.info-box
    %h1= @topic.title
    %p.topic_info
      = link_to @topic.node.name, @topic.node, class: "node"
      %span= "  •  "
      = link_to "knwang", nil, class: "user_link"
      %span= "  •  "
      = "published #{@topic.created_at} ago"
  %section#topic_content.box
    %p= @topic.content

%section#sidebar
  %section#new_topic.box
    = link_to "发布新帖", nil, class: "btn btn-success"
  %section#stats.box
    %ul
      %li 社区会员: 4029 
      %li 帖子数: 312 
      %li 回帖数: 3123 

注意:回复的部分在后面的步骤中做,暂时删掉即可

  • 测试失败:undefined methodtitle' for nil:NilClass`
  • 原因:没有对@topic变量赋值

编辑controllers/topics_controller.rb

def show
  @topic = Topic.find(params[:id])
end
  • 测试失败:undefined methodcontent' for #<Topic:0x0...`
  • 原因:最开始生成topic的时候忘了content属性了 :)

rails console中执行

[5] pry(main)> generate "migration AddContentToTopics content:text"
      invoke  active_record
      create    db/migrate/20130101134732_add_content_to_topics.rb
=> "Completed"
[6] pry(main)>

执行数据库升级

bundle exec rake db:migrate
bundle exec rake db:test:prepare
  • 测试通过!

修改测试用例,增加内容和创建时间的验证

# coding: utf-8
feature '访问者希望看到帖子的详细信息' do
  background do
    @node = Node.create name: "Ruby"
    @topic = Topic.create title: "topic 1 test", content: "topic 1 content",  node: @node
  end

  scenario '访问/topics/1, 应该显示帖子的详细信息' do
    visit "/topics/#{@topic.id}"

    page.should have_content @topic.title
    page.should have_content @topic.content
    page.should have_content @topic.node.name
    page.should have_content "published #{@topic.created_at} ago"
  end
end
  • 测试失败:Can't mass-assign protected attributes: content
  • 原因:没有设置content的访问属性

编辑model/topic.rb

attr_accessible :title, :node, :content
  • 测试通过!

再次确认用户故事

增加导航栏的验证

page.should have_link "Home", href: root_path
page.should have_link @topic.node.name, href: node_path(@topic.node)
page.should have_link "浏览帖子", href: topic_path(@topic)
  • 测试失败:undefined local variable or methodroot_path' for...`n
  • 原因:没有root_path的定义

编辑config/routes.rb,增加root_path的定义

MyRubyChina::Application.routes.draw do
  resources :topics, only: [:index, :show]
  resources :nodes, only: [:show]

  root to: "topics#index"

  match 'ui/:action', controller: 'ui'
...

删除默认的主页文件

rm public/index.html
  • 测试失败:expected to find link "Home" but there were no matches. Als...
  • 原因:没有定义对应的链接

编辑topics/show.html.haml,修改顶部导航栏链接

= render 'shared/breadcrumb', links: [["Home", root_path], [@topic.node.name, @topic.node], ["浏览帖子", @topic]]
  • 测试通过!

完成,提交代码

git add .
git rm public/index.html
git commit 
git checkout dev
git merge f4 --no-ff
git branch -d f4

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

blueplanet in 使用 RSpec+Capybara 简单 BDD 入门 -5 mention this topic. 03 Apr 10:56
blueplanet in 使用 RSpec+Capybara 简单 BDD 入门 -3 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.