0
我又回到了另一個問題。我正在通過Michael Hartl rails教程,並且我似乎陷入了這一部分(我用版本控制重做了三次,並且在這裏得到了相同的結果。)找不到字段第一個水豚元素沒有找到
我得到這個錯誤信息:
2)用戶頁面編輯用有效的信息 故障/錯誤:FILL_IN 「第一」,以:new_first 水豚:: ElementNotFound: 無法找到現場 「第一」 #./spec/requests/ user_pages_spec.rb:57:在'block(4 levels)in'
而且我多次得到同樣的信息。我認爲在這一點上我有大約7個錯誤作爲原因。
所以,這裏的相關代碼:
require 'spec_helper'
describe "User pages" do
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
end
describe "signup page" do
before { visit signup_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 "First", with: "Example"
fill_in "Last", with: "User"
fill_in "Email", with: "[email protected]"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
expect do
click_button "Create my account"
end.to change(User, :count).by(1)
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before { visit edit_user_path(user) }
describe "page" do
specify { page { should have_content("Update your profile") } }
specify { page { should have_title("Edit User") } }
specify { page { should have_link('change', href: 'http://gravatar.com/emails') } }
end
describe "with invalid information" do
before { click_button "Save changes" }
specify { page { should have_content("error") } }
end
describe "with valid information" do
let(:new_first) { "First" }
let(:new_last) { "Last" }
let(:new_email) { "[email protected]" }
before do
fill_in "First", with: new_first
fill_in "Last", with: new_last
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
specify { page { should have_selector("div.alert.alert-success") } }
specify { page { should have_title("new_first") } }
specify { page { should have_link('Sign out', href: signout_path) } }
specify { expect(user.reload.name).to eq new_first }
specify { expect(user.reload.name).to eq new_last }
specify { expect(user.reload.email).to eq new_email }
end
end
end
那是我user_pages規範。這是我的用戶控制器。
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
# Handle a successful update.
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
end
這裏是html頁面本身。
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first %>
<%= f.text_field :first %>
<%= f.label :last %>
<%= f.text_field :last %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a>
</div>
</div>