我刚刚开始第10章,刚好在图10.1之后。
我在迁移和测试之前尝试了rake db:reset:prepare命令,但是没有用。
这是我的错误,我在这里找不到类似的错误:
故障:
1) Micropost
Failure/Error: @micropost = Micropost.new(content: "Lorem ipsum", user_id: user_id)
NameError:
undefined local variable or method `user_id' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcfecc789b8>
# ./spec/models/micropost_spec.rb:9:in `block (2 levels) in <top (required)>'
2) Micropost
Failure/Error: @micropost = Micropost.new(content: "Lorem ipsum", user_id: user_id)
NameError:
undefined local variable or method `user_id' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcfecc8a000>
# ./spec/models/micropost_spec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.00389 seconds
2 examples, 2 failures
Failed examples:
rspec ./spec/models/micropost_spec.rb:15 # Micropost
rspec ./spec/models/micropost_spec.rb:14 # Micropost
这是我的代码:
文件的: db / migrate / [我的唯一时间戳] _create_microposts.rb
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id
t.timestamps
end
add_index :microposts, [:user_id, :created_at]
end
end
对于文件: spec / models / micropost_spec.rb
require 'spec_helper'
describe Micropost do
# pending "add some examples to (or delete) #{__FILE__}"
let(:user) { FactoryGirl.create(:user) }
before do
#This code is not correct, idiomatically
@micropost = Micropost.new(content: "Lorem ipsum", user_id: user_id)
end
subject { @micropost }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
end
PS所有我的测试都是绿色的。 10
答案 0 :(得分:2)
也许
@micropost = Micropost.new(content: "Lorem ipsum", user_id: user_id)
错了? user_id(第二次提及,即价值)来自哪里?
可能是user.user_id
或current_user.id
答案 1 :(得分:2)
来自the tutorial的确切代码:
require 'spec_helper'
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before do
# This code is not idiomatically correct.
@micropost = Micropost.new(content: "Lorem ipsum", user_id: user.id)
end
subject { @micropost }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
end
不是user_id: user_id
而是user_id: user.id