2012-12-08 68 views
1

我失败了3次测试,并且我在这里和在互联网上发现了大量的例子,但是我似乎无法找到我要去哪里的错误。谢谢你的帮助。失败的rspec Rails教程第9.3章

1) User pages signup with valid information edit page 
Failure/Error: before { visit edit_user_path(user) } 
ActionView::Template::Error: 
    undefined method `model_name' for NilClass:Class 
# ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___4113112884365867193_70232486166220' 
# ./spec/requests/user_pages_spec.rb:96:in `block (5 levels) in <top (required)>' 

2) User pages signup with valid information edit page 
Failure/Error: before { visit edit_user_path(user) } 
ActionView::Template::Error: 
    undefined method `model_name' for NilClass:Class 
# ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___4113112884365867193_70232486166220' 
# ./spec/requests/user_pages_spec.rb:96:in `block (5 levels) in <top (required)>' 

3) User pages signup with valid information edit page 
Failure/Error: before { visit edit_user_path(user) } 
ActionView::Template::Error: 
    undefined method `model_name' for NilClass:Class 
# ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___4113112884365867193_70232486166220' 
# ./spec/requests/user_pages_spec.rb:96:in `block (5 levels) in <top (required)>' 

Finished in 0.26515 seconds 
3 examples, 3 failures 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:100 # User pages signup with valid information edit page 
rspec ./spec/requests/user_pages_spec.rb:99 # User pages signup with valid information edit page 
rspec ./spec/requests/user_pages_spec.rb:101 # User pages signup with valid information edit page 

authentication_pages_spec.rb

require 'spec_helper' 

describe "Authentication" do 

subject { page } 

describe "signin page" do 
    before { visit signin_path } 

    it { should have_selector('h1', text: 'Sign in') } 
    it { should have_selector('title', text: 'Sign in') } 
end 

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 

describe "with valid information" do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
    end 



    it { should have_selector('title', text: user.name) } 
    it { should have_link('Profile', href: user_path(user)) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should_not have_link('Sign in', href: signin_path) } 

    describe "followed by signout" do 
    before { click_link "Sign out" } 
    it { should have_link('Sign in') } 
    end 
end 
end 


end 

这里是users_controller:

class UsersController < ApplicationController 

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] = "Welcome to the Sample App!" 
    redirect_to @user 
else 
    render 'new' 
    end 
end 
end 

def edit 
    @user = User.find(params[:id]) 
end  

edit.html.erb:

<% 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 :name %> 
    <%= f.text_field :name %> 

    <%= 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> 

这里是user_pages_spec:

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_selector('h1', text: user.name) } 
it { should have_selector('title', text: user.name) } 
end 

describe "signup page" do 
before { visit signup_path } 

it { should have_selector('h1', text: 'Sign up') } 
it { should have_selector('title', text: full_title('Sign up')) } 
end 

describe "signup" do 

before { visit signup_path } 

describe "with invalid information" do 
it "should not create a user" do 
    expect { click_button "Create my account" }.not_to change(User, :count) 
end 

describe "error messages" do 
    before { click_button "Create my account" } 

    it { should have_selector('title', text: 'Sign up') } 
    it { should have_content('error') } 
end 
end 

describe "with valid information" do 
    before do 
    fill_in "Name", with: "Example User" 
    fill_in "Email", with: "[email protected]" 
    fill_in "Password", with: "foobar" 
    fill_in "Confirmation", with: "foobar" 
end 

it "should create a user" do 
    expect do 
    click_button "Create my account" 
    end.to change(User, :count).by(1) 
end 

describe "after saving the user" do 
    before { click_button "Create my account" } 
    let(:user) { User.find_by_email('[email protected]') } 

    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 "signup page" do 
before { visit signup_path } 

it { should have_selector('h1', text: 'Sign up') } 
it { should have_selector('title', text: 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 "with valid information" do 
before do 
    fill_in "Name", with: "Example User" 
    fill_in "Email", with: "[email protected]" 
    fill_in "Password", with: "foobar" 
    fill_in "Confirmation", with: "foobar" 
end 

it "should create a user" do 
    expect { click_button submit }.to change(User, :count).by(1) 
end 
describe "edit" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { visit edit_user_path(user) } 

    describe "page" do 
    it { should have_selector('h1', text: "Update your profile") } 
    it { should have_selector('title', text: "Edit user") } 
    it { should have_link('change', href: 'http://gravatar.com/emails') } 
    end 

    describe "with invalid information" do 
    before { click_button "Save changes" } 

    it { should have_content('error') } 
    end 
    end 
    end 
    end 
end 

编辑:users_controllers.rb格式不正确。它应该是这样的:

class UsersController < ApplicationController 

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] = "Welcome to the Sample App!" 
    redirect_to @user 
else 
    render 'new' 
end 
end 

def edit 
    @user = User.find(params[:id]) 
end 
end 

回答

0

我在编辑操作之前结束了类UsersController,所以它没有看到它。请参阅我在OP底部的编辑。

2

如果@user变量是不是在你的edit行动,这意味着它不会是可用的视图中的form_for调用设置只会出现该错误......但它被设置,所以它应该可用。

你确定你已经保存了这些文件吗?

+0

是的,我甚至再次拯救他们,以确保。 – jfoutch

2

在你user_pages_spec.rb,您有:

let(:user) { FactoryGirl.create(:user) } 
before { visit edit_user_path(user) } 

相信对于编辑工作,你需要在用户签署这样:

let(:user) { FactoryGirl.create(:user) } 
before do 
    sign_in user 
    visit edit_user_path(user) 
end 

我可以有进一步的编辑雷,但这就是我在查看提交日志时看到的内容。希望这可以帮助。

+0

感谢您的回复,但如果您在我的帖子底部看到编辑内容,我最终会自己找出答案。我无法回答自己的问题,并接受答案,所以我只是编辑。 – jfoutch

相关问题