简体   繁体   中英

Validate presence of telephone number Rspec

I am trying to validate the prescence of telephone numbers using rspec. I have set the telephone number to a string. I have the following code.

Model

  validates_presence_of :name, :address, :telephone, :email
  validates :email, :presence => true,
                    :format   => { :with => email_regex,:message => 'Enter valid email example@example.com ' }
end

Factory

Factory.define :company do |c|
  c.name "Example"
  c.address "123 Shark Road, London, England, SW1 9EP"
  c.telephone "(874)052-1258"  
  c.email "example@example.co.uk"
end

Spec

describe Company do
  before(:each) do
    @company = Factory(:company)
    @attr = {
      :name => "Example",
      :address => "123 Shark Road London England SW1 9EP",
      :telephone => "(874)052-1258",
      :email => "example@example.co.uk"

    }
  end
  it "should create a new instance given valid attributes" do
    Company.create!(@attr)

  end
  it "should commenters name" do
    no_comment_name = Company.new(@attr.merge(:name => ""))
    no_comment_name.should_not be_valid
  end

end

I am getting the following error:

bundle exec rspec spec/models/company_spec.rb /home/dj/.rvm/gems/ruby-1.9.2-p290/gems/ rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load': /home/ / *** /spec/models/com pany_spec.rb:10: syntax error, unexpected tSTRING_BEG, expecting tASSOC (SyntaxError) :telephone " (874)052-1258", ^ /home/ / *** /spec/models/company_spec.rb:10: syntax error, unexpected ',', expecting keyword_end :telephone " (874)052-1258", ^

The error is in your before_each, here :telephone "(874)052-1258". You are missing the => operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM