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开发者_Go百科 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.
精彩评论