我遇到了一些类似性质的错误,我不知道如何修复它们。我遇到的一个问题是教程的新版本与旧版本之间的差异(我知道存在一些差异)。
我的规格是:
Ruby版本:1.9.2p320
Rails版本:3.2.13
Rspec: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
答案 0 :(得分:1)
您在控制器源代码中编写了“当前用户”。我打赌你打算写current_user
。计算机编程中的标识符几乎没有空格。