0

我的micropost_spec.rb 文件中的代码出现以下 3 个错误。我将如何修复它们?我想我完全按照教程进行操作,但不同版本的 Rails 可能会出现问题。

红宝石版本:1.9.2p320

导轨版本:3.2.13

Rspec:2.11.1

电脑:Macbook pro OS X Mountain Lion

错误

1) user_id 不存在时的微博
   失败/错误:它 { User.should_not be_valid }
   无方法错误:
     未定义的方法“有效吗?” 为了 #
   # ./spec/models/micropost_spec.rb:19:in `block (3 levels) in '

2) 内容为空白的微博
   失败/错误:它 { User.should_not be_valid}
   无方法错误:
     未定义的方法“有效吗?” 为了 #
   # ./spec/models/micropost_spec.rb:24:in `block (3 levels) in '

3) 内容过长的微博
   失败/错误:它 { User.should_not be_valid }
   无方法错误:
     未定义的方法“有效吗?” 为了 #
   # ./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
4

1 回答 1

0

您收到的错误消息与您共享的规范文件不匹配。错误消息显示User.should ....,而您的规范有一个隐含的主题。该错误反映您是在间接调用valid?该类User而不是预期的@micropost.

于 2013-08-10T19:36:15.703 回答