【问题标题】:click_link rspec test not finding tag by idclick_link rspec 测试未按 id 找到标签
【发布时间】:2014-01-03 19:22:18
【问题描述】:

我已经在 Michael Hartl 的 Rails 3 教程一书中添加了回复微博功能,但我现在正在尝试为它们编写测试(我知道我是倒着做的)。回复添加有效,但我正在尝试编写以下测试

  1. 创建用户和用户的两个帖子
  2. 从 other_user 创建 other_user 和两个帖子
  3. 让用户关注 other_user
  4. 检查用户的主页提要是否包含 other_user 的帖子
  5. ...

我的测试目前在第四步失败。以下是规范文件中的 sn-p、失败错误以及我的 repo 的链接。 (用户在此描述之前定义,但在此块范围内)

describe "replying to a micropost" do
    let (:other_user) { FactoryGirl.create(:user) }
    let (:first_post) { FactoryGirl.create(:micropost, user: other_user, content: "Whatever.") }
    let (:second_post) { FactoryGirl.create(:micropost, user: other_user, content: "Nevermind.") }

    before do
        user.follow!(other_user)
        visit root_path
    end

    it "should render the posts from other user" do
        page.should have_selector("li##{first_post.id}", text: first_post.content)
        page.should have_selector("li##{second_post.id}", text: second_post.content)
    end


Failures:

  1) Static pages Home page for signed-in users replying to a micropost should render the posts from other user
     Failure/Error: page.should have_selector("li##{first_post.id}", text: first_post.content)
       expected css "li#203" with text "Whatever." to return something
     # ./spec/requests/static_pages_spec.rb:85:in `block (5 levels) in <top (required)>'

Finished in 3.43 seconds
17 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:84 # Static pages Home page for signed-in users replying to a micropost should render the posts from other user

Done.

https://github.com/johnklawlor/sample_app

【问题讨论】:

  • 请分享您遇到的具体故障。
  • 刚刚添加了 rspec 失败...
  • 很抱歉,但没有看到任何明显的内容,也不要向上梳理您的源存储库。 :-) 这很好地解释了为什么要使用小块进行 TDD,但是考虑到您所处的位置,您可能希望从在故障点打印出当前页面内容开始。
  • 如何在故障点打印出当前页面内容?

标签: ruby-on-rails rspec


【解决方案1】:

简而言之,我的测试失败了,因为我需要用户让! (let bang) 为了在 let 的时候创建微博!称呼。使用简单的 let 调用(即不带 !)直到 page.should have_selector 行才创建微博,该行位于访问 root_path 调用之后,因此在页面上找不到微博。

我是这样想的...它与在这个问题中迭代的语句有关 let vs before 初始化,其中他/她说:“对于 let 定义的方法,初始化代码仅在示例调用时运行它。” (来源:stackoverflow.com/questions/5359558/when-to-use-rspec-let)在我的示例中,直到我在 have_selector('li'... , 这意味着它们直到访问 root_path 调用之后才会创建,因此在用户的提要建立之后。我唯一需要支持这个假设的是,如果你添加一些像 first_post.content=first_post.content 这样的行(和在访问 root_path 调用之前,对于 second_post 也是如此,

  • 标签是为帖子生成的,由从 save_and_open_page 打开的页面和通过测试证明。在访问 root_path 之后插入像 first_post.content=first_post.content 这样的行不会产生 li 标签和失败的测试。
  • 【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2014-09-17
      • 2016-11-21
      相关资源
      最近更新 更多