簡體   English   中英

Michael Hartl教程的第10章中的“未定義的方法'valid?'”

[英]“undefined method `valid?'” in chapter 10 of Michael Hartl's tutorial

我在micropost_spec .rb文件中收到以下3個錯誤的代碼。 我該如何解決? 我認為我正在完全按照本教程進行操作,但是不同版本的Rails可能存在問題。

Ruby版本:1.9.2p320

Rails版本:3.2.13

R規格:2.11.1

電腦:Macbook pro OS X Mountain Lion

失誤

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

您收到的錯誤消息與您共享的規范文件不匹配。 錯誤消息顯示User.should .... ,而您的規范包含一個隱式主題。 該錯誤反映您間接調用了valid? 在類User而不是預期的@micropost

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM