首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails教程,第8章rspec错误

Rails教程,第8章rspec错误
EN

Stack Overflow用户
提问于 2013-07-11 21:10:48
回答 1查看 291关注 0票数 0

我是Rails的新手,我正在学习Michael的教程。我开始了第八章,不知道错过了哪一步就失败了。任何帮助都是非常感谢的。

代码语言:javascript
复制
    Failures:

  1) User pages signup with invalid information should not create a user
     Failure/Error: expect { click_button submit }.not_to change(User, :count)
     ActionView::Template::Error:
       undefined method `errors' for nil:NilClass
     # ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
     # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
     # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
     # ./app/controllers/users_controller.rb:23:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:30:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:30:in `block (4 levels) in <top (required)>'

  2) User pages signup with invalid information after submission 
     Failure/Error: before { click_button submit }
     ActionView::Template::Error:
       undefined method `errors' for nil:NilClass
     # ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
     # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
     # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
     # ./app/controllers/users_controller.rb:23:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:33:in `block (5 levels) in <top (required)>'

  3) User pages signup with invalid information after submission 
     Failure/Error: before { click_button submit }
     ActionView::Template::Error:
       undefined method `errors' for nil:NilClass
     # ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
     # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
     # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
     # ./app/controllers/users_controller.rb:23:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:33:in `block (5 levels) in <top (required)>'

users_controller.rb

代码语言:javascript
复制
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def new
    @title = "Sign up"
    @user = User.new

        respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  def create 
    @user = User.new(params[:user])
    if @user.save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
end

user_pages_spec.rb

代码语言:javascript
复制
require 'spec_helper'

describe "User pages" do

  subject { page }

  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 "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" 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
      describe "after submission" do
        before { click_button submit }

        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: "user@example.com"
        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 "after saving the 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') }
      end
    end
  end
end

app/views/shared/_error_messages.html.erb

代码语言:javascript
复制
<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      The form contains <%= pluralize(@user.errors.count, "error")%>
    </div>
    <ul>
    <% @users.errors.full_messages.each do |msg| %>
      <li>* <%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-11 21:22:57

而不是@users在您的_error_messages.html.erb,即nil,您应该有@user

代码语言:javascript
复制
<% @user.errors.full_messages.each do |msg| %>
  <li>* <%= msg %></li>
<% end %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17603585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档