目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -2
访问者希望看到一个节点的帖子列表
git checkout -b f3
rails c # console
rails s # server
spec/features/nodes_spec.rb
# coding: utf-8
feature '访问者希望看到一个节点的帖子列表' do
background do
@node1 = Node.create name: "Ruby Node"
@node2 = Node.create name: "Rails Node"
2.times.map.with_index { |i| Topic.create title: "topic #{i}", node: @node1}
3.times.map.with_index { |i| Topic.create title: "topic #{i + 10}", node: @node2}
end
scenario '点击节点名称后,应该显示该节点的帖子列表' do
visit '/topics'
page.should have_content @node1.name
page.should have_content @node2.name
first(:link, @node1.name).click
page.should have_content @node1.name
page.should_not have_content @node2.name
end
end
Failure/Error: page.should_not have_content @node2.name expected there not to be text "Rails Node" in "....
topics/index.html.haml
= link_to topic.node.name, nil, class: "node"
修改为
= link_to topic.node.name, , class: "node"
undefined method node_path' for ...
node
的路由信息config/routes.rb
,增加node
的路由定义resources :nodes, only: [:show]
uninitialized constant NodesController
rails console
中执行[3] pry(main)> generate "controller nodes"
create app/controllers/nodes_controller.rb
invoke haml
create app/views/nodes
=> "Completed"
[4] pry(main)>
The action 'show' could not be found for NodesController
show
方法controllers/nodes_controller.rb
,增加show
方法class NodesController < ApplicationController
def show
end
end
Missing template nodes/show, application/show with {:...
show
模板views/nodes/show.html.haml
拷贝ui/node.html
至上述文件,并修改第 5 行
%h2= node.name
undefined methodname' for nil:NilClass
@node
赋值controllers/nodes_controller.rb
,增加@node
的赋值@node = Node.find(params[:id])
nodes/show.html.haml
第 7 行%p= @node.description
undefined methoddescription' for #<...
`node
没有description
方法rails console
中执行[4] pry(main)> generate "migration AddDescriptionToNodes description:text"
invoke active_record
create db/migrate/20130101042100_add_description_to_nodes.rb
=> "Completed"
[5] pry(main)>
bundle exec rake db:migrate
bundle exec rake db:test:prepare
controllers/nodes_controller.rb
,修改帖子列表的部分= render 'shared/breadcrumb', links: [["Home", ""], ["社区", ""], ["Ruby", ""]]
%section#node_topics
%section#node_info.box.box-gray.info-box
%h2= @node.name
%span 共有48个讨论主题
%p= @node.description
%ul.topics.box
- @node.topics.each do |topic|
%li
%a.span1(href="")
%img(src="#{gravatar_url('[email protected]')}")
%article.span7
%p.topic_title
= link_to topic.title
%p.topic_info
= link_to topic.node.name, topic.node, class: "node"
%span= " • "
= link_to "knwang", nil, class: "user_link"
%span= " • "
= "published #{time_ago_in_words(topic.created_at)} ago"
%p.replies.span1
%span 12
%section#sidebar
%section#new_topic.box
= link_to "发布新帖", nil, class: "btn btn-success"
%section#stats.box
%ul
%li 社区会员: 4029 人
%li 帖子数: 312 篇
%li 回帖数: 3123 条
undefined methodtopics' for #<Node:...
`node
没有topics
方法model/node.rb
,增加topics
的声明has_many :topics
测试成功!
仔细看一下,topics/index.html.haml
和nodes/show.html.haml
两个页面中,显示帖子列表的部分是完全相同的,可以提出一个部分模板
topics/_topics.html.haml
%ul.topics.box
- topics.each do |topic|
%li
%a.span1(href="")
%img(src="#{gravatar_url('[email protected]')}")
%article.span7
%p.topic_title
= link_to topic.title
%p.topic_info
= link_to topic.node.name, topic.node, class: "node"
%span= " • "
= link_to "knwang", nil, class: "user_link"
%span= " • "
= "published #{time_ago_in_words(topic.created_at)} ago"
%p.replies.span1
%span 12
** 注意:把@topics
变量修改为本地变量topics
**
topics/index.html.haml
把文件中上述部分删除后,增加部分模板的调用
%span 查看: 默认
= render 'topics', topics: @topics
%section#sidebar
%section#new_topic.box
把文件中上述部分删除后,增加部分模板的调用
%span 共有48个讨论主题
%p= @node.description
= render 'topics/topics', topics: @node.topics
%section#sidebar
%section#new_topic.box
注意:传入变量的部分略有不同
spec/features/nodes_spec.rb
,增加description
的设置以及验证# coding: utf-8
feature '访问者希望看到一个节点的帖子列表' do
background do
@node1 = Node.create name: "Ruby Node", description: "Ruby是一门优美的语言"
@node2 = Node.create name: "Rails Node", description: "Rails是一个快速WEB开发框架"
2.times.map.with_index { |i| Topic.create title: "topic #{i}", node: @node1}
3.times.map.with_index { |i| Topic.create title: "topic #{i + 10}", node: @node2}
end
scenario '点击节点名称后,应该显示该节点的帖子列表' do
visit '/topics'
Node.all.each do |node|
page.should have_content node.name
end
first(:link, @node1.name).click
page.should have_content @node1.name
page.should have_content @node1.description
page.should_not have_content @node2.name
page.should_not have_content @node2.description
end
end
Can't mass-assign protected attributes: description
description
的访问属性没有设置好险,这个地方忘记了!
model/node.rb
,增加description
属性声明attr_accessible :name, :description
git add .
git commit
git checkout dev
git merge f3 --no-ff
git branch -d f3