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

blueplanet · 2013年01月05日 · 3454 次阅读

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

用户故事

用户希望看到帖子的回复列表

  • 显示帖子的回复数量
  • 显示回复的下列信息
    • 回复日期时间
    • 回复内容

环境准备

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

步骤

编辑spec/features/guest_can_see_topic_info_spec.rb,增加回复部分的验证

scenario '应该显示帖子的回复信息' do
  visit "/topics/#{@topic.id}"

  page.should have_content "共收到 5 条回复"
end
  • 测试失败:expected there to be text "共收到 5 条回复" in "社区 会员...
  • 原因:还没有实现代码

编辑topics/show.html.haml

拷贝ui/topic.html.haml的回复部分到当前模板

  %section#topic_content.box
    %p= @topic.content

  %section#replies_banner.box.info-box
    %span 共收到 5 条回复

  %section#replies.box
    %ul
      %li
        %a.span1(href="")
          %img(src="#{gravatar_url('[email protected]')}")
        %article.span8
          = link_to "knwang", nil, class: "user_link"
          %span two hours ago
          %p ++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。
      %li
        %a.span1(href="")
          %img(src="#{gravatar_url('[email protected]')}")
        %article.span8
          = link_to "knwang", nil, class: "user_link"
          %span two hours ago
          %p ++ -- 这个操作就是罪恶的源泉。。
      %li
        %a.span1(href="")
          %img(src="#{gravatar_url('[email protected]')}")
        %article.span8
          = link_to "knwang", nil, class: "user_link"
          %span two hours ago
          %p ++ -- 这个操作就是罪恶的源泉。。


%section#sidebar
  %section#new_topic.box

并修改为

  %section#topic_content.box
    %p= @topic.content

  %section#replies_banner.box.info-box
    %span= "共收到 #{@topic.replies.count} 条回复"

  %section#replies.box
    %ul
      - @topic.replies.each do |reply|
        %li
          %a.span1(href="")
            %img(src="#{gravatar_url('[email protected]')}")
          %article.span8
            = link_to "knwang", nil, class: "user_link"
            %span= "#{time_ago_in_words(reply.created_at)} ago"
            %p= reply.content 

%section#sidebar
  %section#new_topic.box

注意:回复的user暂时先不考虑

  • 测试失败:undefined methodreplies' for #Topic:0x007fc0f5c4b3b0...`
  • 原因:没有replies定义

编辑models/topic.rb,增加关联定义

has_many :replies
  • 测试失败:uninitialized constant Topic::Reply
  • 原因:没有Reply这个model的定义

rails console中执行

[26] pry(main)> generate "model reply content:text topic_id:integer"
      invoke  active_record
      create    db/migrate/20130103064335_create_replies.rb
      create    app/models/reply.rb
=> "Completed"
[27] pry(main)>

执行数据库升级

bundle exec rake db:migrate
bundle exec rake db:test:prepare

修改 spec 文件

# 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

    5.times.map.with_index { |i| Reply.create content: "reply #{i}", topic: @topic, user: user }
  end

  scenario '应该显示帖子的详细信息' do
......
  end

  scenario '应该显示帖子的回复信息' do
    visit "/topics/#{@topic.id}"

    page.should have_content "共收到 #{@topic.replies.count} 条回复"

    @topic.replies.each do |reply|
      page.should have_content reply.content
    end
  end
end
  • 测试失败:Can't mass-assign protected attributes: topic, user
  • 原因:没有设置topic,user的访问属性

编辑models/reply.rb,增加访问属性并设置 topic 和 user 的关联

class Reply < ActiveRecord::Base
  attr_accessible :content, :topic

  belongs_to :topic
end
  • 测试通过!

完成,提交代码

git add .
git commit 
git checkout dev
git merge f9 --no-ff
git branch -d f9

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

blueplanet 使用 RSpec + Capybara 简单 BDD 入门 -目录 提及了此话题。 12月19日 16:51
blueplanet 使用 RSpec+Capybara 简单 BDD 入门 -8 提及了此话题。 04月10日 10:20
blueplanet 使用 RSpec+Capybara 简单 BDD 入门 -10 提及了此话题。 04月03日 10:56
需要 登录 后方可回复, 如果你还没有账号请 注册新账号