开发者

Rspec tests with mongoDB doesn't work

开发者 https://www.devze.com 2023-02-15 15:39 出处:网络
I want to develop a small webapp with Ruby on Rails and mongoDB. I use Rails 3.0.3 and the mongoid-gem. Now I want to do some tests with rspec, but it does not work as I expect. The tests give not the

I want to develop a small webapp with Ruby on Rails and mongoDB. I use Rails 3.0.3 and the mongoid-gem. Now I want to do some tests with rspec, but it does not work as I expect. The tests give not the right results. Here is the model I want to test:

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

And here is the test tha开发者_高级运维t doesn't work:

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

Is there something special I must know when I want do to tests with rspec and mongoDB/mongoid?

Thanks for your answers


Try this syntax.

validates_format_of :email, :with => /[A-Za-z]/

This format will obviously fail. Then replace it with your regex.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号