我的 rspec 测试不会通过:Michael Hartl 的 Rails 教程 [英] my rspec test won't pass: Michael Hartl's rails tutorial

查看:33
本文介绍了我的 rspec 测试不会通过:Michael Hartl 的 Rails 教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第五章的结尾做练习.我应该测试链接是否转到正确的页面.这是我的测试代码.

I am at the end of chapter five doing the exercises. I am supposed to be testing that the links go to the correct pages. Here is my test code.

require 'spec_helper'

describe "LayoutLinks" do

    it "should have the right links on the layout" do
        visit root_path
        click_link "About"
        response.should have_selector('title', :content => "About")
        click_link "Home"
        response.should have_selector('title', :content => "Home")
        click_link "Help"
        response.should have_selector('title', :content => "Help")
        click_link "Contact"
        response.should have_selector('title', :content => "Contact")
        click_link "Sign up now!"
        response.should have_selector('title', :content => "Sign up")
        end
        end

除了最后一个测试,一切都通过了.它说找不到带有立即注册!"文本的链接..我知道该页面确实有一个立即注册!"关联.我想可能它在源代码中的呈现方式不同,但是当我查看源代码时,它看起来很正常 <a href="/signup" class="signup_button round">​​ 现在注册!</> .根据我的理解,它应该点击链接,然后测试标题是否与 :content 符号匹配.我是不是误会了什么?

Everything passes except for the last test. It says that it can not find a link with the text "Sign up now!" . I know that the page does have a "Sign up now!" link. I thought that maybe it rendered differently in the source code, but when I look at the source code it looks normal <a href="/signup" class="signup_button round">Sign up now!</a> . From my understanding, it is supposed to click on the links and then test if the title matches the :content symbol. Am I misunderstanding something?

这是我得到的错误:

Failures:

  1) LayoutLinks should have the right links on the layout
     Failure/Error: click_link "Sign up now!"
     Webrat::NotFoundError:
       Could not find link with text or title or id "Sign up now!"

推荐答案

我认为问题在于立即注册!"链接实际上并非在所有页面上,而且此测试实际上会导航页面.

I believe the problem is that the "Sign up now!" link is in fact not on all pages, and this test actually navigates the pages.

至少在我前段时间浏览的本教程版本中,该链接仅在主页上.

At least in the version of this tutorial that I ran through some time ago, that link was only on the home page.

如果您在最后一个 click_link 之前添加一个 visit root_path,它可能会起作用.

If you add a visit root_path just before that last click_link it'll likely work.

更好的测试应该是在与主页相关的独立测试中进行检查.

A better test would have that check in an independent test related specifically to the home page.

这篇关于我的 rspec 测试不会通过:Michael Hartl 的 Rails 教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆