我目前正在通过 Michael Hartl 的Rails Tutorial进行工作,并且当他说应该进行 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 { should 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="✓" /><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>
我计划继续学习本教程,因为看起来功能不受阻碍。但是,我担心以后我对这些测试缺乏了解会严重搞砸事情。如果有人有类似的问题冷帮助指出我的解决方案,那就太好了。
提前致谢!