我目前正在参加Hartl教程的第9章。我刚刚添加了一些测试来检查'编辑'页面工作。测试应该根据教程传递,但其中5个失败。我认为错误可能出在我的user_pages_spec.rb文件中(在下面发布)。根据命令行,它有一些问题:'未定义的局部变量或方法'用户' '
C:\Sites\sample_app>bundle exec rspec spec/
............................................FFFFF
Failures:
1) User pages signup with valid information
Failure/Error: fill_in "Password", with: user.password
NameError:
undefined local variable or method `user' for #<RSpec::Core::ExampleGroup
::Nested_4::Nested_3::Nested_3:0x5259a68>
# ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in <top (requi
red)>'
2) User pages signup with valid information
Failure/Error: fill_in "Password", with: user.password
NameError:
undefined local variable or method `user' for #<RSpec::Core::ExampleGroup
::Nested_4::Nested_3::Nested_3:0x52b0810>
# ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in <top (requi
red)>'
3) User pages signup with valid information
Failure/Error: fill_in "Password", with: user.password
NameError:
undefined local variable or method `user' for #<RSpec::Core::ExampleGroup
::Nested_4::Nested_3::Nested_3:0x5339058>
# ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in <top (requi
red)>'
4) User pages signup with valid information
Failure/Error: fill_in "Password", with: user.password
NameError:
undefined local variable or method `user' for #<RSpec::Core::ExampleGroup
::Nested_4::Nested_3::Nested_3:0x53abec8>
# ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in <top (requi
red)>'
5) User pages signup with valid information
Failure/Error: fill_in "Password", with: user.password
NameError:
undefined local variable or method `user' for #<RSpec::Core::ExampleGroup
::Nested_4::Nested_3::Nested_3:0x5412630>
# ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in <top (requi
red)>'
Finished in 1.03 seconds
49 examples, 5 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:52 # User pages signup with valid infor
mation
rspec ./spec/requests/user_pages_spec.rb:53 # User pages signup with valid infor
mation
rspec ./spec/requests/user_pages_spec.rb:55 # User pages signup with valid infor
mation
rspec ./spec/requests/user_pages_spec.rb:54 # User pages signup with valid infor
mation
rspec ./spec/requests/user_pages_spec.rb:56 # User pages signup with valid infor
mation
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" 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 "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
end
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_title(new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
specify { expect(user.reload.email).to eq new_email }
end
end
end
users.controller.rb
class UsersController < ApplicationController
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)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
答案 0 :(得分:2)
问题出在describe "edit" do
块中。您在那里启动用户变量,但随后在启动describe "with valid information" do
块之前结束该块。因此用户变量不会被传递。
我会在规范的最顶部的before
块中创建用户,如下所示:
require 'spec_helper'
describe "User pages" do
before { user = FactoryGirl.create(:user) }
subject { page }
...
end
执行此操作时,用户变量将可用于您定义的所有规范。这也更清晰,因为你不必一遍又一遍地重新创建它。