2016-12-30 77 views
-4

Hello guys我正在学习如何使用RSpec测试代码,并且在尝试运行Rspec时遇到此错误。我认为这是对活动记录一个错误,但我搜索google搜索,我没有任何的帮助我这个......所以我的错误是这样的:`method_missing':未定义的局部变量或方法'ture'为#<Class:0x007f9922f68110>(NameError)

$ 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: '[email protected]') 
    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: '[email protected]' 
    ) 
    contact = Contact.new(
     first_name: 'Jane', 
     last_name: 'Tester', 
     email: '[email protected]' 
    ) 
    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: '[email protected]') 
    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: '[email protected]' 
    ) 
    jones = Contact.create(
     first_name: 'Tim', 
     last_name: 'Jones', 
     email: '[email protected]' 
    ) 
    johnson = Contact.create(
     first_name: 'John', 
     last_name: 'johnson', 
     email: '[email protected]' 
    ) 
    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: '[email protected]' 
    ) 
    jones = Contact.create(
     first_name: 'Tim', 
     last_name: 'Jones', 
     email: '[email protected]' 
    ) 
    johnson = Contact.create(
     first_name: 'John', 
     last_name: 'Johnson', 
     email: '[email protected]' 
    ) 
    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

'存在:ture',应该是TRUE; – Iceman

+3

这里有一个提示:如果你做一些事情非常正常,非常标准,成千上万的开发人员每天也在使用一个由成千上万的开发人员使用的框架,并且具有数千万行代码,并且以某种方式取决于它,* nobody * has * * ever *注意到有一个错误使得这个框架被成千上万的使用f开发人员*完全无用* ...你错了。 –

回答

5

在你contact.rb你有一个错字

validates :last_name, presence: ture 

应该

validates :last_name, presence: true 
+0

噢我的天啊,傻瓜错误谢谢你们! – rld

相关问题