我已经关注该指南有一段时间了,不幸的是一直无法通过这些测试。感谢您抽出宝贵时间,因为我已经放弃寻找问题所在,迫切需要任何帮助。
Failures:
1) signup with valid information after saving user
Failure/Error: it {should have_link('Sign out')}
expected link "Sign out" to return something
# ./spec/requests/user_pages_spec.rb:97:in `block (4 levels) in <top (required)>'
2) signup with valid information after saving user
Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
expected css "div.alert.alert-success" with text "Welcome" to return something
# ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>'
3) signup with valid information after saving user
Failure/Error: it { should have_selector('title', text: user.name) }
expected css "title" with text "Example User" to return something
# ./spec/requests/user_pages_spec.rb:95:in `block (4 levels) in <top (required)>'
4) create user page
Failure/Error: it {should have_selector('h1', text: 'Creating New User')}
expected css "h1" with text "Creating New User" to return something
# ./spec/requests/user_pages_spec.rb:56:in `block (2 levels) in <top (required)>'
5) create user page
Failure/Error: it {should have_selector('title', text: 'Sign Up')}
expected css "title" with text "Sign Up" to return something
# ./spec/requests/user_pages_spec.rb:57:in `block (2 levels) in <top (required)>'
6) edit with valid information
Failure/Error: it { should have_selector('div.alert.alert-success') }
expected css "div.alert.alert-success" to return something
# ./spec/requests/user_pages_spec.rb:132:in `block (3 levels) in <top (required)>'
7) edit with valid information
Failure/Error: it { should have_selector('title', text: new_name) }
expected css "title" with text "New Name" to return something
# ./spec/requests/user_pages_spec.rb:131:in `block (3 levels) in <top (required)>'
8) edit with valid information
Failure/Error: it { should have_link('Sign out', href: signout_path) }
expected link "Sign out" to return something
# ./spec/requests/user_pages_spec.rb:133:in `block (3 levels) in <top (required)>'
9) edit with invalid info
Failure/Error: it {should have_content('error')}
expected there to be content "error" in "with invalid info"
# ./spec/requests/user_pages_spec.rb:117:in `block (3 levels) in <top (required)>'
10) edit page
Failure/Error: it { should have_selector('h1', text: "Update your profile") }
expected css "h1" with text "Update your profile" to return something
# ./spec/requests/user_pages_spec.rb:111:in `block (3 levels) in <top (required)>'
11) edit page
Failure/Error: it { should have_selector('title', text: "Edit user") }
expected css "title" with text "Edit user" to return something
# ./spec/requests/user_pages_spec.rb:112:in `block (3 levels) in <top (required)>'
12) profile page
Failure/Error: it{should have_selector('h2', text: user.name)}
expected css "h2" with text "Person 66" to return something
# ./spec/requests/user_pages_spec.rb:63:in `block (2 levels) in <top (required)>'
13) profile page
Failure/Error: it{should have_selector('title', text: user.name)}
expected css "title" with text "Person 67" to return something
# ./spec/requests/user_pages_spec.rb:64:in `block (2 levels) in <top (required)>'
我的用户页面规范
require 'spec_helper'
describe "UserPages" do
subject {page}
describe "index" do
before do
sign_in FactoryGirl.create(:user)
FactoryGirl.create(:user, name: "Bob", email: "bob@example.com")
FactoryGirl.create(:user, name: "Ben", email: "ben@example.com")
visit users_path
end
it { should have_selector('title', text: 'All users') }
it { should have_selector('h1', text: 'All Users') }
describe "pagination" do
before(:all) {30.times {FactoryGirl.create(:user)}}
after(:all) {User.delete_all}
it {should have_selector('div.pagination')}
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect { click_link('delete') }.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end
it "should list each user" do
User.all.each do |user|
page.should have_selector('li', text: user.name)
end
end
end
end
describe "create user page" do
before {visit new_user_path}
it {should have_selector('h1', text: 'Creating New User')}
it {should have_selector('title', text: 'Sign Up')}
end
describe "profile page" do
let(:user){FactoryGirl.create(:user)}
before {visit user_path(user)}
it{should have_selector('h2', text: user.name)}
it{should have_selector('title', text: user.name)}
end
describe "signup" do
before { visit new_user_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
choose("user_admin_true")
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving user" do
before {click_button submit}
let(:user) { User.find_by_email('user@example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
it { should have_link('Sign out')}
end
end
end
describe "edit" do
let(:user) {FactoryGirl.create(:user)}
before do
sign_in user
visit edit_user_path(user)
end
describe "page" do
it { should have_selector('h1', text: "Update your profile") }
it { should have_selector('title', text: "Edit user") }
end
describe "with invalid info" do
before {click_button "Save changes"}
it {should have_content('error')}
end
describe "with valid information" do
let(:new_name) { "New Name" }
let(:new_email) { "new@example.com" }
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
it { should have_selector('title', text: new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { user.reload.name.should == new_name }
specify { user.reload.email.should == new_email }
end
end
用户控制器
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Account creation succesful. Welcome to the CeFam Online Database"
redirect_to @user
else
render 'new'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User removed."
redirect_to users_url
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
private
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def signed_in_user
flash[:notice] = "Please sign in." unless signed_in?
redirect_to signin_url unless signed_in?
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
end
标题
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "Image Home Logo", '#', id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<!--Visibile only when signed in-->
<% if signed_in? %>
<li><%= link_to "List users", users_path %></li>
<li><%= link_to "Search", '#' %></li>
<li><%= link_to "Add Entry", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li><%= link_to "Sign out", signout_path, method: "delete" %>
</li>
<%else%>
<li><%= link_to "Sign in", signin_path %></li>
<%end%>
</ul>
</nav>
</div>
</div>
</header>
用户的 show.html.erb
<% provide(:title,@user.name) %>
<h1> User Profile </h1>
<h2><%=@user.name%><br>
<%= @user.email %><br>
Administrator: <%= @user.admin%>
</h2>
用户的 new.html.erb
<% provide(:title,'Sign Up')%>
<h1>Creating New User</h1>
<div class="row">
<div class="span4 offset4">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :admin %>
<%= f.radio_button :admin, "true"%> Yes <br>
<%= f.radio_button :admin, "false"%> No<br><br>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<br>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>