验证电话号码Rspec的存在

时间:2011-08-30 15:01:38

标签: ruby-on-rails ruby-on-rails-3 rspec

我正在尝试使用rspec验证电话号码的确定性。我已将电话号码设为字符串。我有以下代码。

型号

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

工厂

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

规范

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

我收到以下错误:

  

捆绑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:在`load'中:   /首页/ / 的*** /规格/型号/ COM       pany_spec.rb:10:语法错误,意外的tSTRING_BEG,期待   tASSOC(SyntaxError)                :电话“(874)052-1258”,                            ^       /home//***/spec/models/company_spec.rb:10:语法错误,   意外的',',期待keyword_end                :电话“(874)052-1258”,                                            ^

1 个答案:

答案 0 :(得分:2)

错误发生在您的before_each,在这里:电话“(874)052-1258”。您错过了=>运营商。