0
votes

I'm getting the following 3 errors for my code in the micropost_spec.rb file. How would I fix them? I think I'm following the tutorial exactly but it may be a problem with differing versions of Rails.

Ruby version: 1.9.2p320

Rails version: 3.2.13

Rspec: 2.11.1

Computer: Macbook pro OS X Mountain Lion

Errors

1) Micropost when user_id is not present 
   Failure/Error: it { User.should_not be_valid }
   NoMethodError:
     undefined method `valid?' for #
   # ./spec/models/micropost_spec.rb:19:in `block (3 levels) in '

2) Micropost with blank content 
   Failure/Error: it { User.should_not be_valid}
   NoMethodError:
     undefined method `valid?' for #
   # ./spec/models/micropost_spec.rb:24:in `block (3 levels) in '

3) Micropost when content is too long 
   Failure/Error: it { User.should_not be_valid }
   NoMethodError:
     undefined method `valid?' for #
   # ./spec/models/micropost_spec.rb:29:in `block (3 levels) in '

micropost_spec.rb

require 'spec_helper'

describe Micropost do

  let(:user) { FactoryGirl.create(:user) }
  before { @micropost = user.microposts.build(content: "Lorem ipsum") }

  subject { @micropost }

  it { should respond_to(:content) }
  it { should respond_to(:user_id) }
  it { should respond_to(:user) }
  its(:user) { should eq user }

  it { should be_valid }

  describe "when user_id is not present" do
    before { @micropost.user_id = nil }
    it { should_not be_valid }
  end

  describe "with blank content" do
    before { @micropost = " "}
    it { should_not be_valid}
  end

  describe "when content is too long" do
    before { @micropost = "a" * 141 }
    it { should_not be_valid }
  end
end
1
Also, @micropost = "a" * 141 etc are not correct; you should be modifying a property of the Micropost, not setting the value of @micropost. But your test subject is wrong, for some reason. - Dave Newton
Maybe you ment user instead of User ? - scaryguy
@DaveNewton okay is there any other files I can post in order to solve the problem? Thanks for the response - megashigger
@user2469211 Fix the @micropost = ... issue, edit your question to reflect the current content. Post the version of Ruby, Rails, RSpec at least. - Dave Newton
Actually, the tutorial says @micropost.content = "a" * 141. If you search for the text you have, it does not appear. - Peter Alfvin

1 Answers

0
votes

The error messages you are getting do not match the spec file you shared. The error messages show User.should ...., whereas your spec has an implicit subject. The error reflects that you're indirectly calling valid? on the class User rather than the intended @micropost.