我想用Ruby on Rails和mongoDB開發一個小型web應用程序。我使用Rails 3.0.3和mongoid-gem。現在我想用rspec做一些測試,但它不像我預期的那樣工作。測試沒有給出正確的結果。 這裏是我想測試模型:使用mongoDB進行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+\-.][email protected][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 => "[email protected]" }
@unvalid = {:nickname => "Bla"}
end
...
it "should reject invalid email addresses" do
address = "[email protected],com"
invalid_email_user = User.new(@attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
是不是有什麼特別的,我必須知道,當我想要做的使用RSpec測試和mongoDB/mongoid?
感謝您的回答
您可以添加spec_helper的源代碼嗎? – 2011-03-09 16:16:38