目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -1
访问者希望看到帖子的节点
git checkout -b f2
rails c # console
rails s # server
spec/features/guest_can_see_node_name_spec.rb
# coding: utf-8
feature '访问者希望看到帖子的节点名称' do
background do
Topic.delete_all
Topic.create title: "DHH 的公开课"
Topic.create title: "Rails3 中 compass 的 IE 使用问题"
Topic.create title: "这周二上海搞Ruby Tuesday么?"
end
scenario do
visit '/topics'
page.should have_content '瞎扯淡'
end
end
expected there to be text "瞎扯淡" in "社区 会员 knowan...
把这一行
= link_to "分享", nil, class: "node"
修改为
= link_to topic.node.name, nil, class: "node"
undefined methodnode' for #Topic:0x007fa4bc0d96d0
`node
这个方法model/topic.rb
增加
belongs_to :node
undefined methodname' for nil:NilClass
`node
没有值Node
这个model
,在rails console
中执行[5] pry(main)> generate "model node name:string"
invoke active_record
create db/migrate/20121231023808_create_nodes.rb
create app/models/node.rb
=> "Completed"
[6] pry(main)>
node_id
。在rails console
中执行[13] pry(main)> generate "migration AddNodeIdToTopics node_id:integer"
invoke active_record
create db/migrate/20121231030435_add_node_id_to_topics.rb
=> "Completed"
[14] pry(main)>
bundle exec rake db:migrate
bundle exec rake db:test:prepare
# coding: utf-8
feature '访问者希望看到帖子的节点名称' do
background do
Topic.delete_all
Node.delete_all
node = Node.create name: "瞎扯淡"
Topic.create title: "DHH 的公开课", node: node
Topic.create title: "Rails3 中 compass 的 IE 使用问题", node: node
Topic.create title: "这周二上海搞Ruby Tuesday么?", node: node
end
scenario '访问/topics, 应该显示所有帖子的节点名称' do
visit '/topics'
Topic.all.each do |topic|
page.should have_content topic.node.name
end
end
end
Can't mass-assign protected attributes: node
node
设置访问属性model/node.rb
,增加node
的属性声明attr_accessible :name, :node
node
信息spec/features/guest_can_see_all_topics_spec.rb
,增加Node
的赋值Node.delete_all
node = Node.create name: "瞎扯淡"
Topic.create title: "DHH 的公开课", node: node
Topic.create title: "Rails3 中 compass 的 IE 使用问题", node: node
Topic.create title: "这周二上海搞Ruby Tuesday么?", node: node
git add .
git commit
git checkout dev
git merge f2 --no-ff
git branch -d f2