我遇到了一些性质相似的错误,而且不确定如何解决。我遇到的一个问题是本教程的新版本与旧版本之间的差异(我知道有一些差异)。

我的规格是:

Ruby版本:1.9.2p320

Rails版本:3.2.13

R规格:2.11.1

电脑:Macbook Pro OS X Mountain Lion

失误

5) Micropost pages micropost creation with invalid information should not create a micropost
     Failure/Error: expect { click_button "Post" }.not_to change(Micropost, :count)
     NameError:
       undefined local variable or method `user' for #<MicropostsController:0x007fc7a7c64078>
     # ./app/controllers/microposts_controller.rb:5:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/micropost_pages_spec.rb:16:in `block (5 levels) in <top (required)>'
     # ./spec/requests/micropost_pages_spec.rb:16:in `block (4 levels) in <top (required)>'

  6) Micropost pages micropost creation with invalid information error messages
     Failure/Error: before { click_button "Post" }
     NameError:
       undefined local variable or method `user' for #<MicropostsController:0x007fc7a7897e90>
     # ./app/controllers/microposts_controller.rb:5:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/micropost_pages_spec.rb:20:in `block (5 levels) in <top (required)>'

  7) Micropost pages micropost creation with valid information should create a micropost
     Failure/Error: expect { click_button "Post" }.to change(Micropost, :count).by(1)
     NameError:
       undefined local variable or method `user' for #<MicropostsController:0x007fc7a7bdfb98>
     # ./app/controllers/microposts_controller.rb:5:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/micropost_pages_spec.rb:29:in `block (5 levels) in <top (required)>'
     # ./spec/requests/micropost_pages_spec.rb:29:in `block (4 levels) in <top (required)>'


Micropost_controller.rb

class MicropostsController < ApplicationController
  before_filter :signed_in_user

  def create
    @micropost = current user.microposts.build(params[:micropost])
    if @micropost.save
        flash[:success] = "Micropost created!"
        redirect_to root_path
    else
        render 'static_pages/home'
    end
  end

  def destroy
  end
end


micropost_pages_spec.rb

require 'spec_helper'

describe "Micropost pages" do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  describe "micropost creation" do
    before { visit root_path }

    describe "with invalid information" do

      it "should not create a micropost" do
        expect { click_button "Post" }.not_to change(Micropost, :count)
      end

      describe "error messages" do
        before { click_button "Post" }
        it { should have_content('error') }
      end
    end

    describe "with valid information" do

      before { fill_in 'micropost_content', with: "Lorem ipsum" }
      it "should create a micropost" do
        expect { click_button "Post" }.to change(Micropost, :count).by(1)
      end
    end
  end
end

最佳答案

您在控制器源代码中编写了“当前用户”。我敢打赌你打算写current_user。计算机编程中的标识符几乎从来没有空格。

关于ruby-on-rails - Michael Hartl教程的第10章中的“未定义的局部变量或方法'user'”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18175823/

10-11 21:06