`method_missing&#39 ;:未定义的局部变量或方法`ture&#39; for#<class:0x007f9922f68110>(NameError)

时间:2016-12-30 15:57:21

标签: ruby-on-rails ruby rspec

大家好,我正在学习如何使用RSpec测试代码,当我尝试运行Rspec时,我遇到了这个错误。我认为是Active Record上的一个错误,但我搜索谷歌搜索,我没有什么可以帮助我...所以我的错误是这样的:

$ rspec
/Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:26:in `method_missing': undefined local variable or method `ture' for #<Class:0x007f9922f68110> (NameError)
    from /Users/romenigld/workspace/ebooks/everyday_rails_Testing_with_RSpec/contact_app/app/models/contact.rb:6:in `<class:Contact>'
    from /Users/romenigld/workspace/ebooks/everyday_rails_Testing_with_RSpec/contact_app/app/models/contact.rb:1:in `<top (required)>'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:360:in `require_or_load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:184:in `const_missing'
    from /Users/romenigld/workspace/ebooks/everyday_rails_Testing_with_RSpec/contact_app/spec/models/contact_spec.rb:3:in `<top (required)>'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/bin/rspec:22:in `load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/bin/rspec:22:in `<main>'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `eval'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `<main>'

我的contact_spec.rb

require 'rails_helper'

describe Contact do
  it "is valid with a first_name, last_name and email" do
    contact = Contact.new(
      first_name: 'Aaron',
      last_name: 'Sumner',
      email: 'tester@example.com')
    expect(contact).to be_valid
  end

  it "is invalid without a firstname" do
    contact = Contact.new(first_name: nil)
    contact.valid?
    expect(contact.errors[:first_name]).to include("can't be blank")
  end

  it "is invalid without a lastname" do
    contact = Contact.new(last_name: nil)
    contact.valid?
    expect(contact.errors[:last_name]).to include("can't be blank")
  end

  it "is invalid without an email address" do

  end
  it "is invalid with a duplicate email address" do
    Contact.create(
      first_name: 'Joe',
      last_name: 'Tester',
      email: 'tester@example.com'
    )
    contact = Contact.new(
      first_name: 'Jane',
      last_name: 'Tester',
      email: 'tester@example.com'
    )
    contact.valid?
    expect(contact.errors[:email]).to include("has already been taken")
  end

  it "returns a contact's full name as a string" do
    contact = Contact.new(
      first_name: 'John',
      last_name: 'Doe',
      email: 'johndoe@example.com')
    expect(contact.name).to eq 'John Doe'
  end

  it "returns a sorted array of results taht match" do
    smith = Contact.create(
      first_name: 'John',
      last_name: 'Smith',
      email: 'jsmith@example.com'
    )
    jones = Contact.create(
      first_name: 'Tim',
      last_name: 'Jones',
      email: 'tjones@example.com'
    )
    johnson = Contact.create(
      first_name: 'John',
      last_name: 'johnson',
      email: 'jjohnson@example.com'
    )
    expect(Contact.by_letter("J")).to eq [johnson, jones]
  end

  it "omits results that do not match" do
    smith = Contact.create(
      first_name: 'John',
      last_name: 'Smith',
      email: 'jsmith@example.com'
    )
    jones = Contact.create(
      first_name: 'Tim',
      last_name: 'Jones',
      email: 'tjones@example.com'
    )
    johnson = Contact.create(
      first_name: 'John',
      last_name: 'Johnson',
      email: 'jjohnson@example.com'
    )
    expect(Contact.by_letter("J")).not_to include smith
  end
end

我的contact.rb

class Contact < ActiveRecord::Base
  has_many :phones
  #accepts_nested_attributes_for :phones

  validates :first_name, presence: true
  validates :last_name, presence: ture
  validates :email, presence: true, uniqueness: true
  validates :phones, length: { is: 3 }

  def name
    [first_name, last_name].join('')
  end

  def self.by_letter(letter)
    where("last_name LIKE ?", "#{letter}%").order(:last_name)
  end
end

如果有人可以帮助我,我将不胜感激!

1 个答案:

答案 0 :(得分:5)

在你的contact.rb中,你有一个拼写错误

validates :last_name, presence: ture

应该是

validates :last_name, presence: true