我想用Ruby on Rails和mongoDB开发一个小的webapp。我使用Rails 3.0.3和mongoid-gem。现在我想用rspec做一些测试,但它不能像我期望的那样工作。测试结果不正确。 这是我想测试的模型:
class User
include Mongoid::Document
field :nickname, :type => String
field :email, :type => String
field :firstname, :type => String
field :lastname, :type => String
field :birthday, :type => Date
field :picture, :type => String
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :nickname, :presence => true
validates :email, :presence => true,
:format => { :with => email_regex }
end
以下是不起作用的测试:
require 'spec_helper'
describe User do
before(:each) do
@attr = { :nickname => "Example", :email => "user@example.com" }
@unvalid = {:nickname => "Bla"}
end
...
it "should reject invalid email addresses" do
address = "user@foo,com"
invalid_email_user = User.new(@attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
当我想用rspec和mongoDB / mongoid进行测试时,我必须知道一些特别的东西吗?
感谢您的回答
答案 0 :(得分:0)
试试这个语法。
validates_format_of :email, :with => /[A-Za-z]/
这种格式显然会失败。然后用正则表达式替换它。