Ruby on Rails教程 - 获取错误,拉出我的头发

时间:2012-09-03 06:03:18

标签: ruby-on-rails

我跟随[迈克尔哈特尔的教程] [1]并在第7章进行练习,现在有4个错误,我无法弄清楚如何解决我的生活。当我手动测试生产应用程序时,错误根本不存在。所以我不知道我的文字开发是否有什么问题,但是我完全失去了所以我想我会在这里发帖,看看我的总体无声是否让我眼花缭乱...感谢你的帮助! / p>

以下是我收到的4条错误消息:

Failures:

  1) signup with invalid information after submission 
     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:38:in `block (4 levels) in <top (required)>'

  2) signup with invalid information after submission 
     Failure/Error: it { should have_content('error') }
       expected there to be content "error" in "after submission"
     # ./spec/requests/user_pages_spec.rb:39:in `block (4 levels) in <top (required)>'

  3) signup after saving the user 
     Failure/Error: it { should have_selector('title', text: user.name) }
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./spec/requests/user_pages_spec.rb:60:in `block (3 levels) in <top (required)>'

  4) signup after saving the 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:61:in `block (3 levels) in <top (required)>'

Finished in 6.8 seconds
10 examples, 4 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:38 # signup with invalid information after submission 
rspec ./spec/requests/user_pages_spec.rb:39 # signup with invalid information after submission 
rspec ./spec/requests/user_pages_spec.rb:60 # signup after saving the user 
rspec ./spec/requests/user_pages_spec.rb:61 # signup after saving the user 

以下是我的user_pages_spec.rb上的代码:

require 'spec_helper'

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


  [1]: http://ruby.railstutorial.org/

以下是views / users / show.html.erb的模板代码

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>

      </h1>
    </section>
  </aside>
</div>

然后是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(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

end

1 个答案:

答案 0 :(得分:2)

好吧,所有,

我不知道如果没有回答我的问题是在这个论坛上有点夸张的初选,但经过近24小时的睡眠,我解决了这个问题!

经过几次G搜索后,我发现我可以通过&#34; end&#34;来阻止某些变量通过。在错误的地方。事实证明我有两个主要领域。一旦我找到并修复了这些错误,所有错误就会消失。

我现在要拍拍我自己。我希望这有助于将来遇到同样问题的所有新手。