繁体   English   中英

Michael Hartl的Rails教程第8章中的问题-CSS期望

[英]Trouble in Chapter 8 of Michael Hartl's Rails Tutorial - CSS Expects

我目前正在通过Michael Hartl的Rails教程进行工作,当他说应该这样做时,一直无法进行rspec测试。 这些是列出8.10之后的测试。 这是在“登录退出”一章中的内容,麻烦的身份验证测试如下:

require 'spec_helper'

describe "Authentication" do

  subject { page }


  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
        before { click_button "Sign in" }

        it { should have_selector('title', text: 'Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }

        #describe "after visiting another page" do
        #   before { click_link "Home" }
        #   it { should_not have_selector('div.alert.alert-error') }
        #end
    end

end

我认为这两个描述登录测试应该通过,但是“ it {应该具有have_selector('div.alert.alert-error',text:'Invalid')}”测试目前显示:

Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
   expected css "div.alert.alert-error" with text "Invalid" to return something
 # ./spec/requests/authentication_pages_spec.rb:21:in `block (4 levels) in <top (required)>'

我的会话控制器如下:

  class SessionsController < ApplicationController

  def new
  end

 def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
       #I haven't gotten to this tutorial part yet
    else
       flash.now[:error] = "Invalid email/password combination"
       render 'new'
    end
  end

  def destroy
  end
end

当我使用内置的Rails服务器对此进行测试时,浏览器源代码包含:

    <!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->    
  </head>
  <body>
    <header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <a href="/" id="logo">sample app</a>
      <nav>
        <ul class="nav pull-right">
          <li><a href="/">Home</a></li>
          <li><a href="/help">Help</a></li>
          <li><a href="#">Sign in</a></li>
        </ul>
      </nav>
    </div>
  </div>
</header>
    <div class="container">
        <div class="alert alert -error">Invalid email/password combination</div>
      <h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <form accept-charset="UTF-8" action="/sessions" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="Gh9r4Qf0R00A31u69ETFKeAt57nj6Qqai5iuBISWOWE=" /></div>

      <label for="session_email">Email</label>
      <input id="session_email" name="session[email]" size="30" type="text" />

      <label for="session_password">Password</label>
      <input id="session_password" name="session[password]" size="30" type="password" />

      <input class="btn btn-large btn-primary" name="commit" type="submit" value="Sign in" />
</form>

我认为,出于我的目的,这里的关键是:

<div class="container">
    <div class="alert alert -error">Invalid email/password combination</div>
  <h1>Sign in</h1>

我认为应该发生的是,我的测试正在访问此页面,并且应该看到在生成的html中存在警报alert -error分区(带有可接受的文本),并通过了测试。 我知道这没有发生,但是我确实有一个问题是我对这个过程的高度理解是否正确。

我一直在寻找关于stackoverflow的其他教程问题。 尽管存在一些类似的问题,但我无法使用它们来回答我的问题,所以我问这个问题,因为我认为这符合新问题。 我确实看到教程问题提供了很多文件,而我只是想在此处发布相关文件。 但是,这是我当前的routes.rb文件

SampleApp::Application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]

  root to:   'static_pages#home'

  match '/signup',  to:  'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete
  match '/help',    to:  'static_pages#help'
  match '/about',   to:  'static_pages#about'
  match '/contact', to:  'static_pages#contact'
end

这是我当前的new.html.erb文件,其文件路径位于〜/ rails_projects / sample_app / app / views / sessions / new.html.erb中

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

我计划继续学习本教程,因为似乎功能不受阻碍。 但是,我担心以后我对这些测试的理解不足会严重破坏事情。 如果有人遇到类似的问题,帮助我为您提供解决方案,那就太好了。

提前致谢!

从您的网页源代码检查:

<div class="alert alert -error">Invalid email/password combination</div>

目标div具有class alert-error 但是,在您的测试用例中,您期望的是具有类alertalert-error的div。

它{应该具有_selector(' div.alert.alert-error ',文本:'Invalid')}

我认为您在alert-error之间输入了一个额外的空格,它应该是alert-error (没有空格)。

您应该检查布局视图,因为您粘贴的sessions/new.html.erb不包括生成此行HTML的源。

更新 :正如Dylan Markow所评论的那样,您可能已经在<div class="alert alert-<%= key %>">键入了额外的空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM