Question

I have just begun chapter 10, gone through just after the Figure 10.1.

I have tried rake db:reset before my migrate and test:prepare commands, but that has not worked.

Here is my error, I could not find a similar one on here:

Failures:

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 

Here is my code:

for the file: db/migrate/[my unique timestamp]_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

For file: 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 all my tests were green going into ch. 10

Was it helpful?

Solution

The exact code from 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

it's not user_id: user_id but user_id: user.id

OTHER TIPS

Maybe

   @micropost = Micropost.new(content: "Lorem ipsum", user_id: user_id)

is wrong? Where is user_id (the second mention, i.e. the value) coming from?

Perhaps user.user_id or current_user.id

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top